Mikeb
Mikeb

Reputation: 791

Implementing Partial Views

I have a number of Item's that belong to a Group. When your viewing an Item you will see some information about the group it belongs to in a top box component on the page. These items have a number of views for them (Simple Details, Full Details, etc) and this top component will be displayed on each view.

The only way I can think to implement this is the following:

  1. Create a ItemViewModel with the item info
  2. Create a GroupViewModel that contains the info to populate the top component as well as a List of ItemViewModels.
  3. Create a partial view for the top Group component.
  4. Pass the GroupViewModel to each of the item view pages.

I don't really like this approach as it causes the GroupViewModel to be an "uber" ViewModel that gets passed to all the Item views. I would kind of like only ItemViewModels to be passed the the Item views but then what is a clean way for the Item Views to include this top component?

(Side Question: Is to possible to setup the url structure as "/Group/12/Item/43" ??)

Upvotes: 0

Views: 191

Answers (1)

Paweł Staniec
Paweł Staniec

Reputation: 3181

I think your ViewModel should something like

public class SomeViewModel
{
    public Group Group { get; set; }
    public List<Item> Items { get; set; }
}

When your view would look like this :

<!-- top section start -->
@Html.Partial("Details", Model.Group)
<!-- top section end -->

@foreach (Item item in Items)
{
    @Html.DisplayFor(item)
}

Upvotes: 2

Related Questions