views:

39

answers:

1

Hi,

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,

A: 

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.

Circadian
Sorry I can't do that becuase the foreach statement has to come after the header. thanks for your suggestion!
Csharper
@simon That makes no sense at all. The suggestion offers code that is independent of the foreach and (unless I'm missing something) addresses the problem without the need for further change.
Murph
@simon can you provide more code so I can try and provide a better answer. As far as i know this is how i would do it (if i followed the viewdata way)
Circadian
Sorry just been playing arround with the code you suggested and I now have it working here is the final solution: <% var selectedCatergory = (IQueryable<Models.Classifieds_Ads>)ViewData["Classifieds"];%> <h2> <%:selectedCatergory.First().Classifieds_Categories.Category%> </h2>I just had a bit of a "special moment" when I earlier said that it wouldn't work.
Csharper