tags:

views:

31

answers:

2

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"]) { %>
 <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" />
<% } %>
A: 

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..

Filip Ekberg
Sorry I hope this makes sense. What I need is to display the Area_Name in various links within my view. Rather than repeat the same loop each time can I assign Area_Name to a variable and then display that variable in each link?
Jemes
@Jemes It already is a variable, isn't it?
Joe Philllips
A: 

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.

Dav Evans