HiddenFor

(ASP.NET MVC)


Hi,
In this article i am discussing about the HiddenFor control in asp.net mvc. I change the value of hiddenfor on the click of the button  using javascript.

View

@{
    ViewBag.Title = "HiddenForExample";
}

<h2>HiddenForExample</h2>

@model ASP.NETMVCFORUMS.Controllers.ControlsController.ProductsforHidden

@using (Html.BeginForm())
{
@Html.HiddenFor(m=>m.ProductID) <br />
    @Html.TextBoxFor(m=>m.ProductName)<br />
   
    <input type="submit" value="Submit" />
}

<input type="button" onclick="HiddenValue();" id="btn_changevalue" value="Change Value" />

<script type="text/javascript">
    function HiddenValue() {

        document.getElementById('ProductID').value = "459";
    }
</script>


Controller and Model

 public class ProductsforHidden
        {
            public string ProductID { get; set; }
            public string ProductName { get; set; }
        }

        public ActionResult HiddenForExample()
        {

            ProductsforHidden obj = new ProductsforHidden();
            obj.ProductID = "1";
            obj.ProductName = "Orange";
            return View(obj);
        }

        [HttpPost]
        public void HiddenForExample(FormCollection collection)
        {
            string productid = Convert.ToString(collection["ProductID"]);
        }


Image

Ratings


Average Rating: 0.00 by 0 users
Giri Prasad
6/28/2013
6/28/2013
Download PDF

Comments