Let's say I have an Index view. The model I pass in is actually a collection of models, so the Html
property is of type HtmlHelper<List<MyModel>>
. If I want to call extension methods (e.g., Display()
or DisplayFor()
on the individual items in the list, however, I think I need to obtain an HtmlHelper<MyModel>
. But how?
假设我有一个索引视图。我传入的模型实际上是模型的集合,因此Html属性的类型为HtmlHelper
>。如果我想在列表中的各个项目上调用扩展方法(例如,Display()或DisplayFor(),我想我需要获取HtmlHelper
I tried using the HtmlHelper<TModel>
constructor, which looks like this:
我尝试使用HtmlHelper
HtmlHelper<TModel>(ViewContext, IViewDataContainer)
But I'm not having any luck with that. I don't know how to obtain the IViewDataContainer
for the item, and the documentation on these things is very sparse.
但我没有运气。我不知道如何为该项获取IViewDataContainer,并且关于这些东西的文档非常稀疏。
A lot of magic apparently happens when I do...
当我这么做时,显然会发生很多魔法......
return View(List<MyModel>);
...in my controller.
......在我的控制器里
How do I recreate that magic on individual items in a list/collection?
如何在列表/集合中的单个项目上重新创建魔法?
Update
Here is a code snippet to show what I'm trying to accomplish:
这是一个代码片段,显示我正在尝试完成的任务:
foreach(var item in items)
{
var helper = new HtmlHelper<ProjName.MyModel>(ViewContext, ????);
%>
<tr>
<%
foreach(var property in properties)
{
%>
<td>
<%= helper.Display(property.DisplayName) %>
</td>
<%
}
%>
</tr>
<%
}
Basically, I want to populate the cells of a table using the items in the collection. I just need help setting the helper
variable.
基本上,我想使用集合中的项目填充表格的单元格。我只需要帮助设置辅助变量。
3 个解决方案
#1
2
One idea is to use RenderPartial. So create a user control which has the type of your model (MyModel). Then in your main view, add the following code.
一个想法是使用RenderPartial。因此,创建一个具有模型类型(MyModel)的用户控件。然后在主视图中,添加以下代码。
<% Html.RenderPartial("SubView", property); %>
So the main view would look like:
所以主视图看起来像:
foreach(var item in items)
{
%>
<tr>
<%
foreach(var property in properties)
{
%>
<td>
<%= Html.RenderPartial("SubView", property) %>
</td>
<%
}
%>
</tr>
<%
}
%>
Then the sub view would be a strongly typed user control of type . The sub view can then just call the helper and it will be of the correct type:
然后子视图将是类型的强类型用户控件。然后子视图可以只调用帮助程序,它将是正确的类型:
<%= Html.Display(Model.DisplayName) %>
#2
1
Phil Haack to the rescue!
菲尔哈克救援!
http://haacked.com/archive/2010/05/05/asp-net-mvc-tabular-display-template.aspx
#3
0
The page itself is a
IViewDataContainer
. You could try that :
页面本身是一个IViewDataContainer。你可以尝试:
foreach(MyModel m in Model)
{
HtmlHelper helper = new HtmlHelper<MyModel>(ViewContext, this);
...
}
UPDATE
Another idea : you could create a ViewUserControl<MyModel>
and use RenderPartial
to render each item with this control. ViewUserControl<MyModel>
exposes a HtmlHelper<MyModel>
that you can use to render the model
另一个想法是:您可以创建一个ViewUserControl
#1
2
One idea is to use RenderPartial. So create a user control which has the type of your model (MyModel). Then in your main view, add the following code.
一个想法是使用RenderPartial。因此,创建一个具有模型类型(MyModel)的用户控件。然后在主视图中,添加以下代码。
<% Html.RenderPartial("SubView", property); %>
So the main view would look like:
所以主视图看起来像:
foreach(var item in items)
{
%>
<tr>
<%
foreach(var property in properties)
{
%>
<td>
<%= Html.RenderPartial("SubView", property) %>
</td>
<%
}
%>
</tr>
<%
}
%>
Then the sub view would be a strongly typed user control of type . The sub view can then just call the helper and it will be of the correct type:
然后子视图将是类型的强类型用户控件。然后子视图可以只调用帮助程序,它将是正确的类型:
<%= Html.Display(Model.DisplayName) %>
#2
1
Phil Haack to the rescue!
菲尔哈克救援!
http://haacked.com/archive/2010/05/05/asp-net-mvc-tabular-display-template.aspx
#3
0
The page itself is a
IViewDataContainer
. You could try that :
页面本身是一个IViewDataContainer。你可以尝试:
foreach(MyModel m in Model)
{
HtmlHelper helper = new HtmlHelper<MyModel>(ViewContext, this);
...
}
UPDATE
Another idea : you could create a ViewUserControl<MyModel>
and use RenderPartial
to render each item with this control. ViewUserControl<MyModel>
exposes a HtmlHelper<MyModel>
that you can use to render the model
另一个想法是:您可以创建一个ViewUserControl