Search code examples
asp.net-mvcmodel-view-controllerviewdata

Determining the results count of ViewData


Hi I have a view with several User Controls and I pass ViewData to all of them, I would like to know how you would determine the element count by specifying the string key. I understand that you cannot use comparison to an integer because ViewData is an object but I have it setup this way for explaining my question. I have also tried null but The ViewData object is never null, even for results where no data is populated in the ViewData. I.e

In my View

    <%if(ViewData["Test"].Values > 0)
      {
    %>
      <%=Html.RenderPartial("~/Views/UC/Test.ascx", ViewData["Test"])%>
   <%
      }
    %>

Solution

  • If I understood your question correctly, you want to get the count out of an element stored inside the ViewData. The only way to achieve this is by casting it to IEnumerable or IList and then call the Count method.