I ran across an issue where I was expecting Html.HiddenFor to resolve the id from my model and discovered that ModelStateCollection is constructed from the query parameters, post data, as well as your model (as discussed here https://stackoverflow.com/a/8749419/910348).
But what I do not understand is why does Html.DisplayFor resolve differently than HiddenFor. Here is my code:
// Model
public class FooModel
{
public int id { get; set; }
}
// Controller
public ActionResult Foo(int id)
{
var model = new FooModel { id = 111 };
return View(model);
}
// Form
@model MVCProject.Models.FooModel
@using (Html.BeginForm())
{
<fieldset>
<legend>FooModel</legend>
@Html.HiddenFor(model => model.id)
<p>ID: @Html.DisplayFor(model => model.id)</p>
<p><input type="submit" value="Save" /></p>
</fieldset>
}
The resulting HTML for a request /Home/Foo/999:
<input data-val="true" data-val-number="The field id must be a number."
data-val-required="The id field is required." id="id" name="id"
type="hidden" value="999" />
<p>ID: 111</p>
Is this expected behavior that HiddenFor and DisplayFor resolve their lambda expressions to different values? Why?