简体   繁体   English

MVC3视图中的模型计数检查

[英]Model Count check in MVC3 view

Here is my view code: 这是我的查看代码:

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

I have added IF condition to only draw table if model count is equals to 5 but still if model contains no data then it gives error that: 我已将IF条件添加到仅在模型计数等于5的情况下绘制表的状态,但是如果模型不包含任何数据,则它会给出以下错误:

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

What is wrong with IF condition? IF条件有什么问题?

Thanks. 谢谢。

your code will only work if you have exactly 5 CourseRegisterModel . 仅当您有5个CourseRegisterModel您的代码才有效。 This is your issue. 这是你的问题。

why don't you just iterate the model(s) 你为什么不只是迭代模型

@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 you really insist on doing this logic inside the view, then you can use operator precedence and check for the Model containing items. 如果您确实坚持在视图内部执行此逻辑,则可以使用运算符优先级并检查包含项的Model。 Without further addo, edit you line: 无需其他插件,请编辑以下行:

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

to: 至:

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

I would personally do this in my viewModel inside my service or controller class and really flesh out the logic required for this hardcoded Count == 5 existing. 我将亲自在服务或控制器类中的viewModel中执行此操作,并真正充实此硬编码Count == 5存在的逻辑。 You aslo seem to be mixing razon and webforms syntax. 您还似乎在混用razon和webforms语法。

Why are you using <% syntax in if clause, change it to use @ 为什么在if子句中使用<%语法,将其更改为使用@

 @if (Model.Count == 5) 
 {

also at the end change <% } %> to following 最后还要将<%}%>更改为以下内容

 }

If Model is Null then accessing to the count would be throw an exception. 如果Model为Null,则访问该计数将引发异常。 so before that you have to check that if the model is null or not. 因此,在此之前,您必须检查模型是否为null。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM