views:

220

answers:

1

I have a collection of Shelves and each shelf has a collection of products. To optimize reuse I have created a partial view ProductList.ascx that is called as I loop through the list of Shelves. In the partial view I want to have an Add link for each type of product, and need the Shelf Id to do so. Since the partial view is a collection of products and may have no products on that shelf yet, how would I include an add link to the partial that includes the Shelf Id?

Parent View:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<ShelfDetail>>" %>
<% foreach (ShelfDetail shelf in Model)
               { %>

                    <%= Html.Encode(shelf.Name) %> 
                        <%= Html.ActionLink("Delete Shelf", "Delete", "Shelf", new { Id = shelf.Id }, null)%>
                        <%= Html.ActionLink("Edit Shelf", "Edit", "Shelf", new { Id = shelf.Id }, null) %>

                        <% Html.RenderPartial("SoupList", shelf.Soups); %>
                        <% Html.RenderPartial("PieList", shelf.Pies); %>

                        <p><%= Html.Encode(shelf.Description) %></p>
                    <% } %>
        <% } %>

Child Soup View (trying to add the route value where it says "NEED SHELF ID HERE"):

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<SoupList>>" %>

Doors: <%= Html.ActionLink("Add Soup", "Add", "Soup", NEED SHELF ID HERE, null)%>
<% foreach (SoupList soup in Model)
{ %>
    <%= Html.Encode(soup.Flavor) %>
<% } %>
+2  A: 

Change the model type of SoupList to a type which includes both the shelf ID and the list of soups, e.g.:

public class SoupPresentation
{
    public Guid ShelfId { get; set; }
    public IEnumerable<SoupList> Soups { get; set; }
}
Craig Stuntz
Thanks Craig. In doing so wouldn't I have to iterate over them in the Shelf view which keeps me from moving that logic into a partial control? I could be misunderstanding this. Right now the SoupList is a presentation object housing the flattened view of a Soup object, it includes a ShelfId on it. I won't be able to tackle it the way I currently have it is what I am reading.
Ryan H
Yes that's correct Ryan. You'd have to do some transformation on the view model, so instead of them being IEnumerable<Product> they might be something like ShelfProductList, where the id can be part of the collection.
Ben Scheirman
Thanks gents, appreciate the help.
Ryan H