Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle null child Entities in MVC Razor

I have an MVC razor view that iterates over an Orders collection. Each order has a Customer, which can be null.

Trouble is, I get a null reference exception when this is the case.

@foreach (var item in Model) {
<tr>
        <td>
        @Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
        @Html.ActionLink("Delete", "Delete", new { id=item.ID })
    </td>
    <td>
        @item.Number
    </td>
    <td>
        @String.Format("{0:g}", item.ReceivedDate)
    </td>
    <td>
        @item.Customer.Name
    </td>

@item.Customer.Name blows up when item.Customer is null (as you'd expect).

This must be an easy question but haven't been able to find the answer!

What's the best way to deal with this, without setting up a ViewModel ?

Thanks Duncan

like image 902
Duncan Avatar asked Apr 26 '11 17:04

Duncan


3 Answers

Try the following:

<td>        
    @(item.Customer != null ? item.Customer.Name : "")
</td>

Edit: Enclosed to ensure it will work in Razor.

like image 114
Leons Avatar answered Nov 16 '22 20:11

Leons


Firstly you can use built-in html helper Html.DisplayFor(m => m[i].Customer.Name) if you would using for iteration instead of foreach. But this have few downside. You may not have indexer collection property and DisplayFor method get expression parameter and compile it which is costly.

Instead of them you can create your own method that handles this scenario much betterly like below.

public static class Utility
{
    public static TValue NullSafe<T,TValue>(this T obj,Func<T,TValue> value)
    {
        try
        {
            return value(obj);
        }
        catch (NullReferenceException/*Exception can be better choice instead of NullReferenceException*/)
        {
            return default(TValue);
        }
    }
}

Now you can use it happly like

@item.NullSafe(m=>m.Customer.Name)

Making NullSafe method as extension or static is your choice.

like image 5
3 revs Avatar answered Nov 16 '22 18:11

3 revs


A simple if should do the job:

<td>
    @if (item.Customer != null)
    {
        <text>@item.Customer.Name</text>
    }
</td>

This being said and shown, that's only a workaround. The real solution consists in defining and using a specific view model.

like image 4
Darin Dimitrov Avatar answered Nov 16 '22 19:11

Darin Dimitrov