Mr.Smithyyy
Mr.Smithyyy

Reputation: 1329

Displaying model data in view, check for null value

I'm not sure if this is possible in the view or if I would somehow have to check for it in the controller but I have some values that are null and I'm trying to fill up a table but I would like a placeholder such as N/A to show up if there's no values.

In my controller I'm just return the model data of a basic linq query.

var model = from u in db.Users where u.Username == "Bob" select u;

In my view I'm just simply displaying the data in a table

<table class='table table-striped table-bordered table-responsive'>
    <tbody>
        <tr>
            <td>@Html.DisplayFor(model => model.CallNo)</td>
        </tr>
    </tbody>
</table>

Upvotes: 0

Views: 563

Answers (1)

PtrBld
PtrBld

Reputation: 64

you can do something like this in your model:

[DisplayFormat(NullDisplayText = "PLACEHOLDER_VALUE")]
public string CallNo { get; set; }

Doing this you can just use

@Html.DisplayFor(model => model.CallNo)

as usual.

Upvotes: 1

Related Questions