MVC3 passing parent entity in list view
I am fairly new to MVC3. I am trying to achieve the following:
Page with list of sections:
@model IEnumerable<Demo.Models.Section>
I iterate through the sections and display some info. Within each section there is a list of items. Those items are displayed through a partial view:
@model IEnumerable<Demo.Models.StringItem>
In that same partial view I have an actionlink to create a new StringItem. But when creating a new StringItem I need to pass the Section ID that it will belong to. When the secti开发者_如何学Goon already has Items, I can pass the SectionID of the first item in the collection. BUT if the list is empty I have no clue on how to pass the Section ID for which the StringItem needs to be created.
I have tried:
@Html.ActionLink("Create New", "CreateStringItem", ViewContext.ParentActionViewContext.ViewData.Model)
But that will pass all the sections that are on that page, wich makes sense, since the parent view is Ienumerable Section.
What is the way to go forward to pass the parent ID?
Use @model Demo.Models.Section instead of IEnumerable and then access your array of items as Model.Items and id as Model.ID or something.
Or create new model like
class SectionModel {
public int ParentID { get; set; }
public IEnumerable<Demo.Models.StringItem> Items { get; set; }
}
and then use that as model for partial view.
Accessing parent view in this way would be code architecture error.
精彩评论