ASP.NET MVC > Controller

Access form data into controller using parameters in ASP.NET MVC

How to access form data into controller using parameters?


To access the form data into the controller action method, we can also use parameters into the action method.

VIEW PAGE
<h2>Receive With Request Form</h2>
@using (Html.BeginForm("ReceiveWithRequestFormData", "ControllerAndAction"))
{
    <ol>
        <li>
            @Html.Label("Username")
            @Html.TextBox("txtUserName") : user
        </li>
        <li>
            @Html.Label("Password")
            @Html.TextBox("txtPassword") : pass
        </li>
    </ol>
    <input type="submit" value="Login" />
}
<div id="divResult"></div>
CONTROLLER CODE
[HttpPost]
        public ActionResult ReceiveWithParameter(string txtUserName, string
        txtPassword)
        {
            if (txtUserName.Equals("user",
            StringComparison.CurrentCultureIgnoreCase) && txtPassword.Equals("pass",
            StringComparison.CurrentCultureIgnoreCase))
            {
                return Content("Login successful !");
            }
            else
            {
                return Content("Login failed !");
            }
        }

Notice that the parameters name of this action method are same as the name of the form element. The ASP.NET MVC Framework is smart enough to detect the name of the parameters and convert the element value as parameter value.

 Views: 19802 | Post Order: 47



Write for us






Hosting Recommendations