2

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

<body>

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

Thanks

Thomas Mathew
  • 1,151
  • 6
  • 15
  • 30
  • 1
    You need to provide more details if you want a good answer. It's a list of names, but what type is it (a string[], a List, a string...)? If it's an array or a list, you'll have to iterate the object in order to write all the names. What's currently being displayed on the page? – Vedran Jun 15 '11 at 06:44
  • I assume `ViewData["names"]` is a `List`? – Alex R. Jun 15 '11 at 06:44

1 Answers1

5

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.

Alex R.
  • 4,664
  • 4
  • 30
  • 40