In this article I will explain with an example, how to set value in Hidden field created using
Html.Hidden and
Html.HiddenFor helper method using
JavaScript and send to Controller in ASP.Net MVC.
Model
The Model class consists of following property.
public class PersonModel
{
public string Name { get; set; }
}
Controller
The Controller consists of following Action methods.
Action method for handling GET operation
Inside this Action method, simple the View is returned.
Action method for handling POST operation
Inside this Action method, the value of the Hidden Field created using the Html.HiddenFor helper method is fetched using the PersonModel class.
And the value of the Hidden Field created using the Html.Hidden helper method is fetched using the Request.Form collection.
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(PersonModel person)
{
string name = person.Name;
string country = Request.Form["Country"];
return View();
}
}
View
Inside the View, in the very first line the PersonModel class is declared as Model for the View.
The View consists of an HTML Form which has been created using the Html.BeginForm method with the following parameters.
ActionName – Name of the Action. In this case the name is Index.
ControllerName – Name of the Controller. In this case the name is Home.
FormMethod – It specifies the Form Method i.e. GET or POST. In this case it will be set to POST.
The Form consists of two Hidden Fields and two HTML INPUT Buttons.
The Hidden Field for the Name value is created using Html.HiddenFor helper method, while the Hidden Field for the Country value is created using Html.Hidden helper method.
The Hidden Fields are assigned ID attribute using the
HtmlAttributes parameter in their respective Helper functions so that the Hidden Fields can be accessed using
JavaScript.
The
Set button has been assigned with a
JavaScript onclick event handler and
Get button is a
Submit button which will be used for handling Controller’s POST Action method.
Button for setting values
When
Set button is clicked, the
Set JavaScript function is called and the values of
Name and
Country are set in their respective Hidden Fields using
JavaScript.
Button for getting values
When Get button is clicked, the Form is submitted to the Controller’s Action method.
@model Pass_HiddenFields_JavaScript_MVC.Models.PersonModel
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
@Html.HiddenFor(m => m.Name, new { @id = "hfName" })
@Html.Hidden("Country", "", new { @id = "hfCountry" })
<input type="button" id="btnSet" value="Set" onclick="Set();" />
<input type="submit" id="btnGet" value="Get" />
}
<script type="text/javascript">
function Set() {
document.getElementById("hfName").value = "Mudassar Khan";
document.getElementById("hfCountry").value = "India";
}
</script>
</body>
</html>
Screenshot
Downloads