107

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?

Gary Barrett
  • 1,352
  • 4
  • 16
  • 28
JPCF
  • 2,139
  • 5
  • 26
  • 47

4 Answers4

114

It creates a hidden input on the form for the field (from your model) that you pass it.

It is useful for fields in your Model/ViewModel that you need to persist on the page and have passed back when another call is made but shouldn't be seen by the user.

Consider the following ViewModel class:

public class ViewModel
{
    public string Value { get; set; }
    public int Id { get; set; }
}

Now you want the edit page to store the ID but have it not be seen:

<% using(Html.BeginForm() { %>
    <%= Html.HiddenFor(model.Id) %><br />
    <%= Html.TextBoxFor(model.Value) %>
<% } %>

This results in the equivalent of the following HTML:

<form name="form1">
    <input type="hidden" name="Id">2</input>
    <input type="text" name="Value" value="Some Text" />
</form>
Community
  • 1
  • 1
Justin Niessner
  • 229,755
  • 35
  • 391
  • 521
  • in his example above – Bryce Fischer Oct 05 '10 at 19:06
  • 11
    I would say "seen" by the user. Users can "modify" anything they want to, hidden or not. – Craig Stuntz Oct 06 '10 at 12:32
  • @Craig - You're right. I actually though I changed that wording but apparently not. – Justin Niessner Oct 06 '10 at 12:33
  • And it's typically used for the ID (as in the example above), which you generally don't want to display. – RickAndMSFT May 10 '13 at 22:34
  • I'm a bit confused because the view model isn't the controller's parameters. So why would the input's name be generated on the view model's fields? – John Oct 30 '14 at 13:46
  • @John - The `ViewModel` isn't the Controller's parameters. It's the parameter that gets passed from the Controller (which constructs the `ViewModel`) to the View (which renders the model). – Justin Niessner Oct 30 '14 at 14:18
  • @JustinNiessner That's what I wrote, that it's _not_ the controller's parameters. But the expression `Html.HiddenFor(model.Id)`'s result suggests it was. I just wrote a question about it [here](http://stackoverflow.com/questions/26655455/mvc-viewmodels-and-url-generation). – John Oct 30 '14 at 14:21
  • Are there not security implications to exposing IDs? – niico Jun 06 '16 at 09:32
  • @niico - If you've properly secured the rest of your application, no. Knowing the ID of an Entity should pose no danger. – Justin Niessner Jun 06 '16 at 18:04
  • I take your point - although exposing internal IDs - specially if they are not GUIDs - just seems a bit un-necessarily open? – niico Jun 06 '16 at 21:21
  • Just an FYI had I posted the same question today rather then googling it and finding it out somewhere else and it wasnt asked here before I would have been bashed to death. The double standards on this site is overwhelming. – Mike Jun 28 '17 at 14:19
8

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"]);
}
James Still
  • 89
  • 1
  • 1
  • 8
    Although this is technically true, you shouldn't have to do this if the form is strongly typed to a view model. You should be able to access the model directly, which removes the need to do any converting. – Yetti Apr 23 '12 at 14:36
  • @Yetti : Incorrect. There is a good reason the scaffolding mechanism adds the ID field. Remove it from one of my tutorials and use Fiddler to see it's not in the post body. – RickAndMSFT May 10 '13 at 23:33
  • 2
    I'm not sure how the comment is incorrect. If the Model is property *For()*'d in the view, the controller method should be strongly typed, and automatically populated by the ModelBinder. – Erik Philips Jun 11 '14 at 19:56
2

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

Devin Andres Salemi
  • 2,158
  • 3
  • 9
  • 20
0

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

https://msdn.microsoft.com/en-us/library/system.web.mvc.html.inputextensions.hiddenfor(v=vs.118).aspx

Abi
  • 69
  • 1
  • 2