2013-07-29 22 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) 
{ 
//....