ASP.NET MVC: how to deal with null objects in a view?

Should model objects that go into the view be checked for zero before viewing? And if null, create a dummy instance? Or do I need to check View for null?

+5
source share
4 answers

My opinion is that the zero object pattern is a good thing and a trade ;. Using this, you can program your view to deal with Foo objects, and all of them (including zero) will act correctly.

The beauty of this template is that it works regardless of whether the null value is possible only alone or as part of a collection (although the latter case should be, IMHO, very rare).

+2

, null?

if(object == null)
{
return View("notfound");
}
+3

. , db . , 0

<% if (ViewData.Model.Count == 0) { %>
    No results found.
<% } %>
0

, null, , (, , ). , , .

0

All Articles