tags:

views:

521

answers:

5
+4  A: 

On the controller side, you can store the settings in ViewData:

base.ViewData["TitleVisible"] = false;

... in the view:

<% foreach (var item in Model) { %>

<tr>
    <% if ((bool)ViewData["TitleVisible"]){ %>
    <td>
        <%= Html.Encode(item.Title) %>
    </td>
    <%}%>
    <td>
        <%= Html.Encode(item.Capacity) %>
    </td>
    <td>
        <%= Html.Encode(item.Count) %>
    </td>
</tr>

<% } %>
BFree
A: 

Couldn't you store the user's preferences in the property bag and then reference these preferences in your loop?

The row would be made up of three if statements that either drew filled in elements or blank elements.

Bryan Rowe
A: 

Depending on user settings any combination of these columns (Title, Capacity, and/or Count) may be set to not show.

There are plenty of ways you can do this. Depends on how you record and store these conditions.

  <%if(item.ShowTitle){%>  
    <td>
      <%= Html.Encode(item.Title) %>
    </td>
  <%}%>

or

  <%if(Session.Current.ShowTitle){%>  
    <td>
      <%= Html.Encode(item.Title) %>
    </td>
  <%}%>

or create a helper that decides what to show in code:

  <% foreach (var item in Model) { Html.CreateItem(item); }%>

or one of many other ways you could do it.

Will
A: 
<% foreach (var item in Model) { %>
    <tr>
        <td>
            <% if(CONDITION) {
            <%= Html.Encode(item.Title) %>
            <% } else { %>
            &nbsp ;
            <% } %>
        </td>
    </tr>
<% } %>

Remember to account for when the table data is blank, otherwise you'll run into rendering issues. Take out the space in the non breaking space line though.

Brandon
A: 

Alternatively, you could create different views depending on the user's settings, and thus keep the if-logic in the controller. May or may not be a good idea, depending on the rest of the app, but it's something to think about.

mgroves