tags:

views:

169

answers:

4

Should model objects that go to the view be checked for null before going to view? And if null, create a dummy instance? Or should the View check for null?

+2  A: 
Harper Shelby
A: 

You shouldn't need to check for nulls. If your getting your data in your controller via a list, it should only return actual db results as objects. If there are no records you can always check for a 0 count in your view and display a message, along the lines of

<% if (ViewData.Model.Count == 0) { %>
    No results found.
<% } %>
rXc3NtR1c
+3  A: 

How about returning a different view if the object is null?

if(object == null)
{
return View("notfound");
}
Skiltz
A: 

It's a special case when your Model is null - so you should either throw an exception or create a default Model (or maybe return a special View). I think you should always provide a Model instance to View if it require a Model.

eu-ge-ne