MVC4 Razor - @Html.DisplayFor not binding to model

后端 未结 6 1757
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-08 07:15

I am trying to find me feet with MVC4 Razor and I\'m stuck with this simple problem.

When I use @Html.DisplayFor the model is always sent back as NULL, but when I u

6条回答
  •  长情又很酷
    2020-12-08 07:45

    DisplayFor will not do the Model binding. TextBoxFor will do because it creates a input element in the form and the form can handle it when it is being posted. If you want to get some data in the HttpPost action and you dont want to use the TextBoxFor, you can keep that pirticulare model proeprty in a hidden variable inside the form using the HiddenFor HTML helper method like this.

    @using(Html.BeginForm())
    {
      

    The Type Name is

    @Html.DisplayFor(x=>x.TypeName) @Html.HiddenFor(x=>x.TypeName) }

提交回复
热议问题