Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if ViewData is null before setting DropDownList to it

Tags:

asp.net-mvc

I have this:

<% if (ViewData["roots"] != null) {%>
       <%Html.DropDownList("roots"); %>
<%}%>

But it's not working. How to check if ViewData exists?

like image 587
user1322207 Avatar asked Apr 09 '12 16:04

user1322207


People also ask

Can ViewData be null?

ViewData is used to pass data from controller to corresponding view. Its life lies only during the current request. If redirection occurs then its value becomes null.

Is TempData stored in session?

It is stored in session storage, but there is one crucial difference between TempData and Session : TempData is available only for a user's session, so it persists only till we have read it and gets cleared at the end of an HTTP Request.

What does the ViewData property do?

The ViewData property is a dictionary of weakly typed objects. The ViewBag property is a wrapper around ViewData that provides dynamic properties for the underlying ViewData collection. Note: Key lookups are case-insensitive for both ViewData and ViewBag . ViewData and ViewBag are dynamically resolved at runtime.


1 Answers

Try

ViewData.ContainsKey("roots")
like image 84
jorgehmv Avatar answered Sep 27 '22 18:09

jorgehmv