I have this:
<% if (ViewData["roots"] != null) {%>
<%Html.DropDownList("roots"); %>
<%}%>
But it's not working. How to check if ViewData exists?
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.
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.
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.
Try
ViewData.ContainsKey("roots")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With