繁体   English   中英

零模型的部分视图

[英]Partial views with null models

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

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

有时课程不会包含任何学生。 因此,无数学生将无效。 问题是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>

是否有允许这样的扩展?

有几个选项,您可以使用空列表初始化您的模型,或者在您拥有呈现部分代码的HTML中,只需执行空检查,如果有如下值,则仅渲染部分:

@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")
}

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

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

确保你的IEnumerable模型不是null ...说出类似的东西

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

暂无
暂无

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

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