ViewData["s"].ToString() vs. Request.QueryString["s"].ToString(), what is returned if "s" was never set?
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开发者_如何学JAVA: Looks like they act the same after all.
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.
精彩评论