我有一个视图,其中包含许多较小的部分视图,用于显示许多不同类型的表格数据。
它使用一个包含许多子模型的模型,例如班级将是模型,学生将是一个子模型或嵌套类。
有时,一个班级不会包含任何学生。因此,学生的可枚举对象将为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>
有没有扩展程序可以实现这个功能?