Search code examples
asp.net-mvcasp.net-mvc-3asp.net-mvc-4asp.net-mvc-2

Why DisplayFor does not post values to Action Method?


When we post the form using below to Action Method, we can see the View Model values in Parameter.

@Html.EditorFor(model => model.Foo)

When we post the form using below to Action Method, we can't see the View Model values in Parameter.

@Html.DisplayFor(model => model.Foo)

So, in later case, w could us Hidden Fields. So, I think, w should not us DisplayFor when it comes o post form values.

Question : Why DisplayFor does not post values to Action Method? Can we discuss the internal mechanism for this ?


Solution

  • see the html rendering. does not have the attribute name. or not an input tag. the form tag only send tag elements input, select, textarea,... that have the attribute name. For example:

    <form...>
       <input id="lastname" name="lastname" />
    </form>
    

    not this:

    <form...>
       <input id="lastname" />
    </form>
    

    enter image description here