什么会我在这种情况下使用(asp.net MVC 3显示模板) [英] what would I use in this situation(asp.net mvc 3 display templates)
问题描述
我有这样的事情
public class ViewModel1
{
// some properties here
public List<ViewModel2> ViewModel2 {get; set;}
}
public class ViewModel2
{
public string A {get; set;}
public string B {get; set;}
}
// view
<table>
<thead>
<tr> a </tr>
<tr> b </tr>
</thead>
<tbody>
// want to use a display template to render table row and cells so I don't need to use for loop
</tbody>
</table>
我试图用 @ Html.DisplayForModel()
,但我似乎采取的视图的视图模型(所以在我的情况ViewModel1)
I tried to use "@Html.DisplayForModel()
" but that I seems to take the viewModel of the view(so in my case ViewModel1)
我要让它走ViewModel2,但我没有看到任何选项ViewModel2(模型对象)来传递。
I need to make it take ViewModel2 but I don't see any option to pass in ViewModel2(a model object).
然后我试图
@Html.DisplayFor(x => x.ViewModel2)
这是没有工作很好,它只是打印出来像第一属性值,甚至从来没有所做的任何细胞。
That did not work to well it just printed out like the first properties value and never even made any cells.
下面基本上是我的显示模板
Here is basically my display template
@model ViewModel2
<tr>
<td>@Model.A</td>
<td>@Model.B</td>
</tr>
所以,我怎样才能使这项工作?
So how can I make this work?
推荐答案
尝试这样的:
<table>
<thead>
<tr>
<th>a</th>
<th>b</th>
</tr>
</thead>
<tbody>
@Html.DisplayFor(x => x.ViewModel2)
</tbody>
</table>
,然后在〜/查看/共享/ DisplayTemplates / ViewModel2.cshtml
:
@model ViewModel2
<tr>
<td>@Model.A</td>
<td>@Model.B</td>
</tr>
注意显示模板的名称和位置。它,如果你想要这个工作尊重这个约定是非常重要的。
Notice the name and location of the display template. It is important to respect this convention if you want this to work.
这篇关于什么会我在这种情况下使用(asp.net MVC 3显示模板)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!