C# MVC3视图中的模型计数检查
以下是我的查看代码:C# MVC3视图中的模型计数检查,c#,asp.net-mvc-3,C#,Asp.net Mvc 3,以下是我的查看代码: @model IEnumerable<StudentRegistrationPortal.Models.CourseRegisterModel> @{ ViewBag.Title = "Welcome Student"; } <h2>Welcome @Context.User.Identity.Name </h2> @Html.ActionLink("[Sign Out]", "SignOut", "Student") <
@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>
<% } %>
如果模型计数等于5,我将IF条件添加到仅绘制表格中,但如果模型不包含数据,则给出错误:
索引超出范围。必须为非负数且小于集合的大小。
参数名称:索引
IF条件有什么问题
谢谢。只有当您正好有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>
}
为什么要使用如果您确实坚持在视图中执行此逻辑,那么可以使用运算符优先级并检查包含项的模型。无需进一步添加,编辑您的行:
<%if (Model.Count == 5) { %>
我个人会在我的服务或控制器类中的viewModel中这样做,并真正充实这个硬编码计数==5所需的逻辑。您似乎还混合了razon和webforms语法。如果模型为Null,则访问计数将引发异常。因此在此之前,您必须检查模型是否为null
@if(Mode != null && Mode.Count == 5)
{
//....
如果计数不等于5,则页面上不应显示任何内容…@walkhard它将显示nothing@NidaSulheri我建议你将其用于或foreachloop@NikolayKostov对,傻我:
<%if (Model.Count == 5) { %>
// check will only continue if Model.Any() evaluates to true
@if (Model.Any() && Model.Count == 5) { ... }
@if(Mode != null && Mode.Count == 5)
{
//....