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 jQuery and send to Controller in ASP.Net MVC.
Note: For beginners in ASP.Net MVC, please refer my article ASP.Net MVC Hello World Tutorial with Sample Program example.
 
 

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 is created using the Html.Hidden helper function 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 methods so that the Hidden Fields can be accessed using jQuery.
Inside the View, the following jQuery script is inherited:
1. jquery.min.js
The Set button been assigned with a jQuery click event handler and Get button is a Submit button which will be used for handling Controller’s POST Action method.
Button for Set values
When Set button is clicked, the values of Name and Country are set in their respective Hidden Fields using jQuery.
 
Button for Get values
When Get button is clicked, the Form is submitted to the Controller’s Action method.
@model Pass_HiddenFields_jQuery_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" />
        <input type="submit" id="btnGet" value="Get" />
    }
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
    <script type="text/javascript">
        $("[id*=btnSet]").on("click", function () {
            $("[id*=hfName]").val("Mudassar Khan");
            $("[id*=hfCountry]").val("India");
        });
    </script>
</body>
</html>
 
 

Screenshot

ASP.Net MVC: Set value of Hidden field using jQuery and send to Controller
 
 

Downloads