Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC: How to Deal with Null objects in View?

Tags:

asp.net-mvc

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?

like image 427
zsharp Avatar asked May 18 '09 02:05

zsharp


People also ask

How to handle null values in asp net MVC?

Type = model. Type. Trim(); whenever null values coming from the database occurs.

Can we pass data from view to controller?

There are four ways to pass the data from View to Controller which are explained below: Traditional Approach: In this approach, we can use the request object of the HttpRequestBase class. This object contains the input field name and values as name-value pairs in case of the form submit.


2 Answers

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

if(object == null)
{
return View("notfound");
}
like image 147
Skiltz Avatar answered Oct 19 '22 18:10

Skiltz


My opinion is that the Null Object pattern is a Good Thing™. Using this, you can code your View to deal with Foo objects, and all of them (including the null one) will act right.

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

like image 2
Harper Shelby Avatar answered Oct 19 '22 17:10

Harper Shelby