零模型的部分视图

时间:2014-04-02 08:39:49

标签: c# asp.net asp.net-mvc razor

我有一个视图,其中包含许多较小的部分视图,以显示许多不同类型的表格数据。

它使用包含许多子模型的模型。例如教室将成为模型,学生将成为子模型或嵌套类。

有时课程不会包含任何学生。因此,无数学生将无效。问题是partials不允许null对象因此抛出异常。

这是一个例子......

主要观点:

@Html.Partial("Partials/_Students", Model.Students)

部分视图

<div class="col-xs-12 col-sm-5 col-md-5 col-lg-5 widget-container-span">
<div class="widget-box">
    <div class="widget-header header-color-dark">
        <h5 class="bigger lighter">
            <i class="icon-table"></i>
            Notes
        </h5>
        <div class="widget-toolbar">
            <a class="plus-sign-link" href ="@Url.Action("AddNewStudent", "Classroom")"><i class="icon-plus-sign bigger-170"></i></a>
        </div>
    </div>
    <div class="widget-body">

        <div class="widget-main no-padding">

            <table class="table table-striped table-bordered table-hover" data-provides="rowlink">

                <thead class="thin-border-bottom">
                    <tr>
                        <th><i class="icon-user"></i>Contact Name</th>
                        <th>Email </th>
                        <th>Telephone</th>
                        <th class="hidden-480">Mobile</th>
                        <th class="hidden-480"></th>
                    </tr>
                </thead>

                @foreach (var item in Model)
                {
                    <tbody data-link="row" class="rowlink">
                        <tr>
                            <td>
                                <a href="@Url.Action("Classroom", "StudentDetails", new { @siteid = item.SiteId, @id = item.Id })">@item.Name</a>
                            </td>
                            <td class="rowlink-skip">
                                <a href="mailto:@item.EmailAddress">@item.EmailAddress</a>
                            </td>
                            <td class="rowlink-skip">
                                <a href="tel:@item.TelephoneNumber">@item.TelephoneNumber</a>
                            </td>
                            <td class="hidden-480 rowlink-skip">
                                <a href="tel:@item.MobileNumber">@item.MobileNumber</a>
                            </td>
                            <td class="hidden-480 rowlink-skip">
                                @if (item.IsDefault)
                                { <span style="display: block;" class="label label-success">Default</span> }
                                else
                                { <a style="display:block;" class="btn btn-minier btn-info" data-id="@item.Id">Make default</a> }
                            </td>
                        </tr>
                    </tbody>
                }
            </table>
        </div>
    </div>
</div>

是否有允许此扩展?

3 个答案:

答案 0 :(得分:6)

有两个选项,您可以使用空列表初始化您的模型,或者在您拥有呈现部分代码的HTML中,只需执行空检查,只有在存在如下值的情况下才渲染部分:< / p>

@if(Model.Students != null && Model.Students.Any()){
    @Html.Partial("Partials/_Students", Model.Students)
}

如果没有学生并且您想通知最终用户,则可以选择呈现不同的部分:

@if(Model.Students != null && Model.Students.Any()){
    @Html.Partial("Partials/_Students", Model.Students)
} else {
    @Html.Partial("Partials/_NoStudents")
}

答案 1 :(得分:4)

您可以添加条件以在主视图中检查子模型是否为空/空,如下所示:

@if(Model.Students != null && Model.Students.Any()){
    @Html.Partial("Partials/_Students", Model.Students)
}

或者您可以在部分视图中添加条件。

主视图

@Html.Partial("Partials/_Students", Model.Students)

<强> PartialView

    @if(Model != null && Model.Any())
   { 
       // HTML code of Partial View
   }
   else
   {
      //Message to display no student record found.
   }

答案 2 :(得分:0)

确保您的IEnumerable模型不为null ...比如说

@if(Model != null)
{
 // render partial view 
}