MVC3 动态列表视图
在 MVC3 中,我已经能够依靠 Html.DisplayForModel() 为我的数据生成显示。使用这种方法和各种模板,我有一个用于显示多个模型的视图。但我想知道,有没有办法让它在我的模型的列表上工作?
例如,我有一个名为网络的模型。我列出多个网络的视图如下所示:
@model PagedList<Network>
<div id="networkList">
@Html.Grid(Model).Columns(column => {
column.For(x => Html.ActionLink(x.Id.ToString(), "NetworkDetails", new { id = x.Id })).Named("Network ID");
column.For(x => x.Name);
column.For(x => x.Enabled);
}).Attributes(Style => "text-align: center")
@Html.AjaxPager(Model, new PagerOptions() { PageIndexParameterName="page", ShowDisabledPagerItems = false, AlwaysShowFirstLastPageNumber=true },
new AjaxOptions() { UpdateTargetId = "networkList" })
</div>
我想知道在为我的模型生成列表时是否可以使用单个模板。我可以依靠属性来知道我想在列表中生成哪些属性,即:[ListItem]。
让我头疼的是,如何将动态模型传递给扩展方法?如果有任何帮助,Html.Grid 扩展来自 MVCContrib。还有其他人做过类似的事情吗?依赖模板会很棒,因为它确实会减少代码量。
In MVC3, I've been able to rely on Html.DisplayForModel() to generate display's for my data. Using this method, and various templates, I have a single View for displaying several of my Models. What I'm wondering though, is there a way I can get this to work on Lists for my models?
For example, I have a model called Networks. My view to list out multiple networks looks like this:
@model PagedList<Network>
<div id="networkList">
@Html.Grid(Model).Columns(column => {
column.For(x => Html.ActionLink(x.Id.ToString(), "NetworkDetails", new { id = x.Id })).Named("Network ID");
column.For(x => x.Name);
column.For(x => x.Enabled);
}).Attributes(Style => "text-align: center")
@Html.AjaxPager(Model, new PagerOptions() { PageIndexParameterName="page", ShowDisabledPagerItems = false, AlwaysShowFirstLastPageNumber=true },
new AjaxOptions() { UpdateTargetId = "networkList" })
</div>
I'm wondering if it is possible to use a single template when generating lists for my models. I could rely on attributes to know which properties I would like to generate in my list, ie: [ListItem].
The head scratcher for me is, how can I pass a dynamic model to an extension method? If it's of any help, the Html.Grid extension is from MVCContrib. Has anyone else done something similar? It would be great to rely on a template as it would really chop down on the amount of code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用以下代码为 EditorFor() 实现它(它可能与您的 Grid 扩展方法类似,假设它可以采用模板名称参数):
或者您可以只传递模板的名称并在模板中使用此代码
You can achieve it for EditorFor() using the following (it might be similar with your Grid extension method assuming it can take the template name parameter):
Alternatively you can just pass in the name of the template and use this code in the template