Alternative way to use ViewData in the View of an ASP.Net MVC Application
I am have been working on an ASP.NET Application. When passing data from the Controller to the View I have been doing the following-
<%foreach(myModel classified in(IQueryable<MyModel>)ViewData["Classifieds"])
{%开发者_运维技巧>
<p><span class="Bold">Title:</span> <%: classified.Header %></p>
<p><span class="Bold">Price: £ </span><%: classified.Price %></p>
<%}%>
As you might guess this is my main content. A load of adverts. Now I want the category that the classified are in as the header of my page. What other way is there istead of going foreach... so it only appears once.
Thanks,
If all adverts in the viewdata have the same category then you get the category from the first object of the viewdata.
((Classifieds)ViewData["Classifieds"][0]).Category
or something like that.
精彩评论