MVC4 Razor - @Html.DisplayFor not binding to model

后端 未结 6 1760
佛祖请我去吃肉
佛祖请我去吃肉 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:49

    I know this is a bit of an old question but you can roll your own, custom combined display control as shown below. This renders the model value followed by a hidden field for that value

    @Html.DisplayExFor(model => Model.ItemCode)
    

    Simply use what the framework already has in place

    public static MvcHtmlString DisplayExFor(this HtmlHelper htmlHelper, Expression> ex)
    {
        var sb = new StringBuilder();
        sb.Append(htmlHelper.DisplayFor(ex));
        sb.Append(htmlHelper.HiddenFor(ex));
    
        return MvcHtmlString.Create(sb.ToString());
    }
    

提交回复
热议问题