2013-07-29 21 views
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条件?

谢谢。

+0

如果计数不等于5再没有什么应该页面上显示... – Azeem

+0

@walkhard它会显示什么 –

+0

@Nida Sulheri我建议你使用或foreach循环 –

回答

3

只有您有5 CourseRegisterModel,您的代码才能正常工作。这是你的问题。

你为什么不只是你为什么要使用if语句,将其更改<%语法使用@

@if (Model.Count == 5) 
{ 

还会在最后迭代模型(一个或多个)

@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> 

} 
0

将<%}%>更改为以下

} 
2

如果您确实坚持这样做gic在视图内,那么你可以使用运算符优先级并检查包含模型的项目。如果没有进一步的阿多,编辑你行:

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

到:

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

我会亲自做这在我的ViewModel我的服务或控制器类中,真正充实这个硬编码计数所需的逻辑= = 5现有。你似乎混合razon和webforms语法。

1

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

@if(Mode != null && Mode.Count == 5) 
{ 
//....