渲染MVC3在循环中的局部视图 [英] Rendering Partial Views in a Loop in MVC3
问题描述
我有一个pretty简单的场景,我的观点是型号列表。
I have a pretty simple scenario, Model for my view is a List.
通过列表循环就像
@foreach(CustomObject obj in Model)
{
Html.Partial("_TrackingCustomObject",obj)
}
所以我期待根据我的名单有部分景色数量。
So i was expecting to have number of partial views according to my list.
部分景观已制定了相应的。
Partial View has been developed accordingly.
有是页面上没有错误。它只是不表明应该通过局部视图来显示任何数据。
There is no error on page. It just does not show any data that is supposed to display by partial views.
什么是不显示任何数据的原因是什么?
What is the reason of not showing any data?
推荐答案
您缺少一个@:
@foreach(CustomObject obj in Model)
{
@Html.Partial("_TrackingCustomObject", obj)
}
但为什么写foreach循环时,你可以使用编辑器/显示模板?像这样的:
But why writing foreach loops when you can use editor/display templates? Like this:
@model IEnumerable<CustomObject>
@Html.EditorForModel()
,然后简单地定义相应的编辑模板(〜/查看/共享/ EditorTemplates / CustomObject.cshtml
),将自动被渲染为模型的每个元素:
and then simply define the corresponding editor template (~/Views/Shared/EditorTemplates/CustomObject.cshtml
) that will automatically be rendered for each element of your model:
@model CustomObject
<div>
@Html.EditorFor(x => x.Foo)
</div>
简单和常规: - )
Simple and conventional :-)
这篇关于渲染MVC3在循环中的局部视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!