0

这是我的视图代码:

@model IEnumerable<StudentRegistrationPortal.Models.CourseRegisterModel>
@{
    ViewBag.Title = "Welcome Student";
}

<h2>Welcome 
@Context.User.Identity.Name
</h2>
@Html.ActionLink("[Sign Out]", "SignOut", "Student")
<ul>
    <li>@Html.ActionLink("Register Courses", "registerCourses", "Course")</li>
</ul>

<%if (Model.Count == 5) { %>
<table border="1">
<tr>
    <th>
        RollNumber
    </th>
    <th>
        Course Code
    </th>
    <th>
        Course Name
    </th>
    <th>Add/Drop</th>
</tr>

<tr>
    <td>
        @Context.User.Identity.Name
    </td>
    <td>
        @Html.DisplayFor(modelItem => Model.ElementAt(0).Course.Code)
    </td>
    <td>
        @Html.DisplayFor(modelItem => Model.ElementAt(0).Course.Name)
    </td>
    <td>
        @Html.ActionLink("Drop", "Drop", new { id=-1 })
    </td>
</tr>

<tr>
    <td>
        @Context.User.Identity.Name
    </td>
    <td>
        @Html.DisplayFor(modelItem => Model.ElementAt(1).Course.Code)
    </td>
    <td>
        @Html.DisplayFor(modelItem => Model.ElementAt(1).Course.Name)
    </td>
    <td>
        @Html.ActionLink("Drop", "Drop", new { id=-1 })
    </td>
</tr>

<tr>
    <td>
        @Context.User.Identity.Name
    </td>
    <td>
        @Html.DisplayFor(modelItem => Model.ElementAt(2).Course.Code)
    </td>
    <td>
        @Html.DisplayFor(modelItem => Model.ElementAt(2).Course.Name)
    </td>
    <td>
        @Html.ActionLink("Drop", "Drop", new { id=-1 })
    </td>
</tr>

<tr>
    <td>
        @Context.User.Identity.Name
    </td>
    <td>
        @Html.DisplayFor(modelItem => Model.ElementAt(3).Course.Code)
    </td>
    <td>
        @Html.DisplayFor(modelItem => Model.ElementAt(3).Course.Name)
    </td>
    <td>
        @Html.ActionLink("Drop", "Drop", new { id=-1 })
    </td>
</tr>

<tr>
    <td>
        @Context.User.Identity.Name
    </td>
    <td>
        @Html.DisplayFor(modelItem => Model.ElementAt(4).Course.Code)
    </td>
    <td>
        @Html.DisplayFor(modelItem => Model.ElementAt(4).Course.Name)
    </td>
    <td>
        @Html.ActionLink("Drop", "Drop", new { id=-1 })
    </td>
</tr>

</table>
<% } %>

我添加了 IF 条件,仅在模型计数等于 5 时才绘制表格,但如果模型不包含数据,则它会给出以下错误:

Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

IF条件有什么问题?

谢谢。

4

4 回答 4

3

只有当您正好有 5 时,您的代码才会起作用CourseRegisterModel。这是你的问题。

你为什么不迭代模型

@foreach(StudentRegistrationPortal.Models.CourseRegisterModel modelValue in Model)
{
<tr>
    <td>
        @Context.User.Identity.Name
    </td>
    <td>
        @Html.DisplayFor(modelItem => modelValue.Course.Code)
    </td>
    <td>
        @Html.DisplayFor(modelItem => modelValue.Course.Name)
    </td>
    <td>
        @Html.ActionLink("Drop", "Drop", new { id=-1 })
    </td>
</tr>

}
于 2013-07-29T16:29:31.280 回答
2

如果您真的坚持在视图中执行此逻辑,那么您可以使用运算符优先级并检查包含项目的模型。无需进一步添加,编辑您的行:

<%if (Model.Count == 5) { %>

到:

// check will only continue if Model.Any() evaluates to true
@if (Model.Any() && Model.Count == 5) { ... }

我会亲自在我的服务或控制器类中的 viewModel 中执行此操作,并真正充实此硬编码 Count == 5 现有所需的逻辑。您似乎也在混合 razon 和 webforms 语法。

于 2013-07-29T16:36:15.923 回答
1

为什么在 if 子句中使用 <% 语法,将其更改为使用 @

 @if (Model.Count == 5) 
 {

最后也将 <% } %> 更改为以下

 }
于 2013-07-29T16:35:04.397 回答
1

如果 Model 为 Null,则访问计数将引发异常。所以在此之前你必须检查模型是否为空。

@if(Mode != null && Mode.Count == 5)
{
//....
于 2013-07-29T16:42:48.613 回答