我已經看到這個問題的很多版本,但答案總是變成「你不需要那樣做」,並且永遠不會有答案。mvc razor中的嵌套編輯模板
我有一個關於產品的屬性列表,我想在無序列表中顯示帶有複選框的特定屬性。
在我的模型:
public List<ProductAttribute> ProductAttributes {get;set;}
在我Create.cshtml:
<div Class="ProductAttributes">
@Html.EditorFor(m => m.ProductAttributes, "ProductAttributeSelectorList")
</div>
在我ProductAttributeSelectorList.cshtml:
@model List<Models.DisplayLocationAttribute>
<div class="AttributeSelector">
<ul>
@foreach (var item in Model)
{
<li>
@Html.EditorFor(_ => item, "EditLocationAttributeList")
</li>
}
</ul>
</div>
最後,在我的EditLocationAttributeList.cshtml
@model Models.DisplayLocationAttribute
@Html.HiddenFor(m => m.Id)
@Html.CheckBoxFor(m => m.IsSelected)
<a href="#" alt="@Model.Description" >@Model.Name</a>
這一切都完美地顯示在頁面上,我可以使用CSS來設計它的樣式,但是當提交返回時,我的model.ProductAttributes集合爲null。
我知道我可以直接綁定到EditLocationAttributeList,它會顯示並返回一個人口model.ProductAttributes如果我用這個:
@Html.EditorFor(m => m.ProductAttributes, "EditLocationAttributeList")
,但現在我沒有到無序列表,我想有。我可以像處理項目模板一樣對待模板,並將行項目標記嵌入到該模板中,但看起來很臭,並且模板與另一個模板緊密耦合。
任何想法?
由於提前,
塔爾
這樣做的竅門,我不清楚發生了什麼。所以我建立了一系列的屬性,我只是提供一個魔術可以使用的數組呢? – TalMcMahon
是的,或多或少。欲瞭解更多詳情,請閱讀Phil Haack的帖子http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx – counsellorben