用剃刀语法访问修改后的闭包?

时间:2011-10-31 08:10:44

标签: c# asp.net-mvc asp.net-mvc-3 razor resharper

在以下代码中,我从Reshaper收到此警告。我想知道我是否必须在代码中更改某些内容,或者只是隐藏此类型的所有警告。

警告位于每个DisplayFor行

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Telephone)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Skypeuser)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.ApplicantID }) |
            @Html.ActionLink("Details", "Details", new { id=item.ApplicantID }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.ApplicantID })
        </td>
    </tr>
}

2 个答案:

答案 0 :(得分:2)

您可以放心地忽略该警告。

这就是说我会在你的视图中用显示模板替换这个foreach循环:

@model IEnumerable<MyViewModel>
<table>
    <thead>
        <tr>
            <th>Name</th>
            <th>Telephone</th>
            <th>Skypeuser</th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        @Html.DisplayForModel()
    </tbody>
</table>

然后定义将为集合的每个元素(~/Views.Shared/DisplayTemplates/MyViewModel.cshtml)自动呈现的相应显示模板:

@model MyViewModel
<tr>
    <td>
        @Html.DisplayFor(x => x.Name)
    </td>
    <td>
        @Html.DisplayFor(x => x.Telephone)
    </td>
    <td>
        @Html.DisplayFor(x => x.Skypeuser)
    </td>
    <td>
        @Html.ActionLink("Edit", "Edit", new { id = Model.ApplicantID }) |
        @Html.ActionLink("Details", "Details", new { id = Model.ApplicantID }) |
        @Html.ActionLink("Delete", "Delete", new { id = Model.ApplicantID })
    </td>
</tr>

不再有警告。

答案 1 :(得分:0)

此警告是错误,并在ReSharper 6.1中修复