Although I have read the documentation on Html.HiddenFor, I've not grasped what is it used for...
Could somebody explain its uses and give a short example?
Where should those helpers go in the code?
This question is related to
.net
asp.net-mvc
asp.net-mvc-2
html-helper
html.hiddenfor
The Use of Razor code @Html.Hidden or @Html.HiddenFor is similar to the following Html code
<input type="hidden"/>
And also refer the following link
Like a lot of functions, this one can be used in many different ways to solve many different problems, I think of it as yet another tool in our toolbelts.
So far, the discussion has focused heavily on simply hiding an ID, but that is only one value, why not use it for lots of values! That is what I am doing, I use it to load up the values in a class only one view at a time, because html.beginform creates a new object and if your model object for that view already had some values passed to it, those values will be lost unless you provide a reference to those values in the beginform.
To see a great motivation for the html.hiddenfor, I recommend you see Passing data from a View to a Controller in .NET MVC - "@model" not highlighting
And to consume the hidden ID input back on your Edit action method:
[HttpPost]
public ActionResult Edit(FormCollection collection)
{
ViewModel.ID = Convert.ToInt32(collection["ID"]);
}
Source: Stackoverflow.com