微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

asp.net-mvc-4 – 复杂模型上的MVC Html.DisplayNameFor

我有一个复杂类型的模型,即用户/地址/地址列表
public class User{
public List<Address> Addresses{get;set;}
}

在我的视图中我显示了这个

<fieldset><legend>@Html.displayNameFor(model=>model.Addresses)</legend>
<table>       
<tr>
    <th>
        @Html.displayNameFor(m => Model.Addresses.Street)
    </th>
    <th>
        @Html.displayNameFor(model => model.Addresses.City)
    </th>
<th>
        @Html.displayNameFor(model => model.Addresses.State)
    </th>

    </tr>
@foreach (var item in Model.Addresses) {    
<tr>
    <td>
        @Html.displayFor(m => item.Street)
    </td>
    <td>
        @Html.displayFor(m => item.City)
    </td>
    <td>
        @Html.displayFor(m => item.State)
    </td>
</tr>
} 
    </table></fieldset>

displayNameFor Street / City / State不起作用.

在子对象上获取displayNameFor的正确方法是什么?

TIA
Ĵ

解决方法

我变了
@Html.displayNameFor(m => Model.Addresses.Street)

@Html.displayNameFor(m => Model.Addresses.FirstOrDefault().Street)

不要使用@ Html.displayNameFor(m => Model.Addresses [0] .Street)因为如果你需要编辑第一行,你将无法这样做.

现在一切都按预期工作了.

原文地址:https://www.jb51.cc/aspnet/248951.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐