views:

37

answers:

1

If in my controller:

public ActionResult Index()
{
     //no code implied
     return View;
}

Then in the view that is returned:

<%if(ViewData["SomeString"].ToString() != "True") {%> show this <%}%>

I will get an error at runtime because of an object reference having no object.

However inside of a page where I do:

<%if(Request.QueryString["Something"].ToString() != "True") {%> show this <%}%>

Update: I actually do get the error.

Edit: Looks like they act the same after all.

+3  A: 

Both ViewData and QueryString will return null for non-existent key. When you're trying to call a method (in your case, ToString) on a null object reference, you get a NullReferenceException.

I'm not sure what's not clear in this situation.

Fyodor Soikin
actually I think I made a mistake here, editing...
shogun
Edited the answer.
Fyodor Soikin
Thanks, this makes sense now. The NullReferenceException is caused by the ToString() call not the comparison (if ToString is excluded it will not error out when comparing to a string even if non-existent key is referenced).
shogun