0

In the following code I am getting this warning from Reshaper. I wonder if I must change something in the code, or just hide all warnings from this type.

The warning is in each DisplayFor line

@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>
}
Luis Valencia
  • 32,619
  • 93
  • 286
  • 506
  • 2
    See: [Outer Variable Trap](http://stackoverflow.com/questions/3416758/outer-variable-trap) – dtb Oct 31 '11 at 08:13

2 Answers2

2

You can safely ignore the warning.

This being said I would replace this foreach loop in your view by a display template:

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

and then define the corresponding display template that will automatically be rendered for each element of the collection (~/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>

No more warnings.

Jhonny D. Cano -Leftware-
  • 17,663
  • 14
  • 81
  • 103
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
0

That warning is bug here and fixed in ReSharper 6.1

derigel
  • 3,218
  • 2
  • 19
  • 31