简体   繁体   中英

How do i display a variable in a <p> element after requesting the data from a POST request using razor(C#) and html

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.

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>

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM