Search code examples
asp.net-mvcviewdata

How to display list of names present in viewdata in asp.net


I have a list of names in viewdata, but am not able to display it in view page

<body>

    <%= ViewData["names"].ToString() %>

Thanks


Solution

  • If it's a list of strings, you have to iterate through it like so (I've put a div there just to illustrate):

    <% foreach (var name in (List<String>)ViewData["names"]) { %>
       <div><%= name %></div>
    <% } %>
    

    You also must make sure that ViewData["names"] is not null.