Displaying ViewData in a View
What I need is to display Area_Name in various links within my view. Rather than repeat the same loop below each time, can I assign Area_Name to a variable and then display that variable in each link within my view?
ViewData
filterContext.Controller.ViewData["Categories"] = from m in _dataContext.Categories where m.Area_ID == SectionID select m;
View Page
Stylesheet
<%
foreach (var c in (IEnumerable<Categories>)ViewData["Categories"]) { %>
<link href="../../Content/<%= c.Area_Name %>/Site.css" rel="stylesheet" type="text/css" />
<% } %>
Image
<%
foreach (var c in (IEnumerable<Categories>)ViewData["Categories"]) { 开发者_JS百科%>
<img src="../../Content/images/<%= c.Area_Name %>/slide1.jpg" />
<img src="../../Content/images/<%= c.Area_Name %>/slide2.jpg" />
<img src="../../Content/images/<%= c.Area_Name %>/slide3.jpg" />
<% } %>
perhaps create a strongly typed viewData file with a property for each AreaName that you have. The downside of this is that when you add a new category the viewData class and the view will not pick up those changes in the same way they will with your current code.
Use a second for-each or a for-loop?
Like this:
<% foreach (var c in (IEnumerable<Categories>)ViewData["Categories"]) {
for ( int i = 0 ; i < 100; i ++ )
{
%>
<img src="../../Content/images/<%= c.Area_Name %>/slide<%= i %>.jpg" />
<%
}
<% } %>
It's hard to know exactly what you mean though..
精彩评论