ASP. NET Method for obtaining Form form value in MVC controller

  • 2021-08-05 09:32:48
  • OfStack

This paper gives an example of how ASP. NET obtains the value of Form form in MVC controller. Share it for your reference, as follows:

In the MVC controller, if we want to get the value of a tag element in the form directly, we can use the FormCollection class provided in MVC, and the specific usage is as follows:

View section:


@using (Html.BeginForm())
{
  <text> The value you entered is: </text><span>@ViewBag.FormValue</span>
  <input type="text" name="txtName" id="txtName" value="" />
  <input type="submit" name="btnSave" id="btnSave" value=" Mention   Hand in " />
}

Controller section:


[HttpPost]
public ActionResult Index(FormCollection formCol)
{
  ViewBag.FormValue = formCol["txtName"];
  return View();
}

For more readers interested in asp. net, please check the topics on this site: "asp. net Optimization Skills Summary", "asp. net String Operation Skills Summary", "asp. net Operation XML Skills Summary", "asp. net File Operation Skills Summary", "asp. net ajax Skills Summary" and "asp. net Cache Operation Skills Summary".

I hope this paper is helpful to everyone's asp. net programming.


Related articles: