0

I noticed that this would work if i don't use Request["name"]; But obviously I need to get the information from my form. I want to display the 'name' in a confirmation message. I am using visual studio asp.net empty webapp

This is the razor code

 @{
    string title = "This is the title";

    string name = "";
    if (IsPost)
    {

        name = Request["name"];
        string email = Request["email"];
        string message = Request["message"];
    }
}


<p>@name</p>

let me know if i should include any other code, but i think this is all you need to see.

inDepth
  • 121
  • 1
  • 13

2 Answers2

0

I figured out a much easier solution then the other answers.

@{
    string title = "This is the title";

    var names = "";
    var emails = "";

    if (IsPost)
    {
        var name = Request["name"];
        var email = Request["email"];
        string message = Request["message"];

        names = name;
        emails = email;
    }
}

By reinitializing the Request["name"]; into another variable, it allows me to use the names variable or emails variable in my html. This makes the use of MVC redundant and is more simple.

<p>Name: @names</p>
inDepth
  • 121
  • 1
  • 13
-1

Controller :

public ActionResult Test()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Test(string Name, string Message, string email)
    {
        ViewBag.Name = Name;
        ViewBag.Message = Message;
        ViewBag.email = email;
        return View();
    }

Razor view:

 @using (Html.BeginForm())
{
    <input type="text" name="Name" />
    <input type="text" name="Message" />
    <input type="email" name="email" />
    <input type="submit" value="submit" />
}

<p>@ViewBag.Name</p><br>
<p>@ViewBag.Message</p><br>
<p>@ViewBag.email<p>
saurav singh
  • 438
  • 6
  • 16
  • This doesnt work, probably because i am working in an asp.net web app environment – inDepth Apr 16 '18 at 05:44
  • sorry I was misunderstood now check again I edited my answer. I hope this is will help you. thank you – saurav singh Apr 16 '18 at 05:47
  • this is the error i get ''HtmlHelper' does not contain a definition for 'BeginForm' and no extension method 'BeginForm' accepting a first argument of type 'HtmlHelper' could be found (are you missing a using directive or an assembly reference?)' – inDepth Apr 16 '18 at 05:52
  • which version of Mvc are you using. it was 100% tested code on MVC5..which is good to go. – saurav singh Apr 16 '18 at 05:54
  • Maybe i should have specified that im using visual studio, my bad. Im using the asp.net blank web app in visual studio – inDepth Apr 16 '18 at 05:56
  • ok mate..!! you can "Upvote" of this answer it will be helpful for others in future.!! thanks. – saurav singh Apr 16 '18 at 05:57
  • I will as soon as the answer proves useful. Right now it doesnt work – inDepth Apr 16 '18 at 05:59
  • use this namespace "using System.Web.Mvc;" on your controller. – saurav singh Apr 16 '18 at 06:00
  • yea doesnt change anything. The controller isnt highlighted, its all white, so clearly its not being recognized – inDepth Apr 16 '18 at 06:03
  • Figured out an much easier solution that doesnt require MVC. I posted it as an answer – inDepth Apr 16 '18 at 19:10