Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between Html.Hidden and Html.HiddenFor

I can find a good definition for Html.HiddenFor on MSDN but the only thing I can find on Html.Hidden is related to problems it has.

Can someone give me a good definition and an example.

like image 776
Joe Pitz Avatar asked Dec 07 '10 21:12

Joe Pitz


People also ask

What is HTML HiddenFor?

HiddenFor() is a strongly typed method that is bounded with model class. It communicates and send/receive value to model class properties. Generally it contains 2 parameters; Hidden Field Name which is a model property and Value for Hidden Field.

What is hidden field in ASP NET MVC?

Here we will learn how to create or use hidden fields in asp.net mvc with a simple example using HTML helpers. Before going in-depth, let's understand the definition of hidden fields. The hidden fields are controls that allow us to store data or information on a page without displaying it.

How do I change the hidden field value in ViewBag?

The Controller consists of two Action methods. Inside this Action method, the value of the Name is set in a ViewBag object which will be later set in the Hidden Field created using Html. Hidden helper function. Inside this Action method, the value of the Hidden Field created using the Html.

What is HTML Labelfor?

Html.Label gives you a label for an input whose name matches the specified input text (more specifically, for the model property matching the string expression): // Model public string Test { get; set; } // View @Html. Label("Test") // Output <label for="Test">Test</label>


2 Answers

Most of the MVC helper methods have a XXXFor variant. They are intended to be used in conjunction with a concrete model class. The idea is to allow the helper to derive the appropriate "name" attribute for the form-input control based on the property you specify in the lambda. This means that you get to eliminate "magic strings" that you would otherwise have to employ to correlate the model properties with your views. For example:

Html.Hidden("Name", "Value") 

Will result in:

<input id="Name" name="Name" type="hidden" value="Value"> 

In your controller, you might have an action like:

[HttpPost] public ActionResult MyAction(MyModel model)  { } 

And a model like:

public class MyModel  {     public string Name { get; set; } } 

The raw Html.Hidden we used above will get correlated to the Name property in the model. However, it's somewhat distasteful that the value "Name" for the property must be specified using a string ("Name"). If you rename the Name property on the Model, your code will break and the error will be somewhat difficult to figure out. On the other hand, if you use HiddenFor, you get protected from that:

Html.HiddenFor(x => x.Name, "Value"); 

Now, if you rename the Name property, you will get an explicit runtime error indicating that the property can't be found. In addition, you get other benefits of static analysis, such as getting a drop-down of the members after typing x..

like image 93
Kirk Woll Avatar answered Sep 22 '22 04:09

Kirk Woll


The Html.Hidden creates a hidden input but you have to specify the name and all the attributes you want to give that field and value. The Html.HiddenFor creates a hidden input for the object that you pass to it, they look like this:

Html.Hidden("yourProperty",model.yourProperty);  Html.HiddenFor(m => m.yourProperty) 

In this case the output is the same!

like image 39
Marco Leo Avatar answered Sep 24 '22 04:09

Marco Leo