decoder
decoder

Reputation: 926

Check system.nullreferenceexception

My code as follows:

@{var UName = ((IEnumerable<Pollidut.ViewModels.ComboItem>)ViewBag.UnionList).FirstOrDefault(x => x.ID == item.UNION_NAME_ID).Name;<text>@UName</text>

if ViewBag.UnionList is empty then it troughs system.nullreferenceexception.How to check and validate this?

Upvotes: 0

Views: 648

Answers (2)

Henk Holterman
Henk Holterman

Reputation: 273429

First of all, you should not be doing this kind of work in the View. It belongs in the Controller. So the cshtml should simply be:

<text>@ViewBag.UName</text>

And in the controller, use something like:

var tempUnion = UnionList.FirstOrDefault(x => x.ID == item.UNION_NAME_ID);
ViewBag.UName = tempUnion == null ? "" : tempUnion.Name;

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1502016

Well, you're calling FirstOrDefault - that returns null (or rather, the default value for the element type) if the sequence is empty. So you can detect that with a separate statement:

@{var sequence = (IEnumerable<Pollidut.ViewModels.ComboItem>)ViewBag.UnionList;
  var first = sequence.FirstOrDefault(x => x.ID == item.UNION_NAME_ID); 
  var name = first == null ? "Some default name" : first.Name; }
<text>@UName</text>

In C# 6 it's easier using the null conditional operator, e.g.

var name = first?.Name ?? "Some default name";

(There's a slight difference here - if Name returns null, in the latter code you'd end up with the default name; in the former code you wouldn't.)

Upvotes: 2

Related Questions