Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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:</p> <pre><code>Html.Hidden("Name", "Value") </code></pre> <p>Will result in:</p> <pre><code>&lt;input id="Name" name="Name" type="hidden" value="Value"&gt; </code></pre> <p>In your controller, you might have an action like:</p> <pre><code>[HttpPost] public ActionResult MyAction(MyModel model) { } </code></pre> <p>And a model like:</p> <pre><code>public class MyModel { public string Name { get; set; } } </code></pre> <p>The raw <code>Html.Hidden</code> we used above will get correlated to the <code>Name</code> 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 <code>Name</code> 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 <code>HiddenFor</code>, you get protected from that:</p> <pre><code>Html.HiddenFor(x =&gt; x.Name, "Value"); </code></pre> <p>Now, if you rename the <code>Name</code> 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 <code>x.</code>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload