Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Partial views with null models

I have a view that contains lots of smaller partial views to display many different types of tabular data.

It uses a model that contains many sub models. e.g. classroom would be the model, students would be a sub-model or nested class.

sometimes a class would not contain any students. thus the ienumerable of students would be null. The problem is that partials do not allow null objects thus throwing an exception.

Here is an example....

Main view:

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

Partial view

<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>

Is there an extension to allow this?

like image 621
James Andrew Smith Avatar asked Dec 20 '22 16:12

James Andrew Smith


2 Answers

You can add the condition to check whether the sub-model is null/Empty in Main view as follows:

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

OR else you can add the condition in Partial view.

Main View

@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.
   }
like image 34
Shivkant Avatar answered Jan 01 '23 01:01

Shivkant


There a couple of options, you could initialize your Model with empty lists or, in the HTML where you have the code that renders your partials, just do a null check and only render the partial if there are values like this:

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

This gives you the option of rendering a different partial if there are no students and you want to inform the end user:

@if(Model.Students != null && Model.Students.Any()){
    @Html.Partial("Partials/_Students", Model.Students)
} else {
    @Html.Partial("Partials/_NoStudents")
}
like image 152
connectedsoftware Avatar answered Jan 01 '23 03:01

connectedsoftware