MVC3 动态列表视图

发布于 2024-10-25 00:50:29 字数 931 浏览 3 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哭泣的笑容 2024-11-01 00:50:29

您可以使用以下代码为 EditorFor() 实现它(它可能与您的 Grid 扩展方法类似,假设它可以采用模板名称参数):

// in the main view
@Html.EditorFor(o => o.InvoiceId, "TemplateName", new { Property = "InvoiceId" })
@Html.EditorFor(o => o.Title, "TemplateName", new { Property = "Title" })

// in the template view
@model object
@{ var property = (string)this.ViewData["Property"]; }

或者您可以只传递模板的名称并在模板中使用此代码

var prefix = ViewData.TemplateInfo.HtmlFieldPrefix;
if (prefix.EndsWith("InvoiceId")) { ... }

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):

// in the main view
@Html.EditorFor(o => o.InvoiceId, "TemplateName", new { Property = "InvoiceId" })
@Html.EditorFor(o => o.Title, "TemplateName", new { Property = "Title" })

// in the template view
@model object
@{ var property = (string)this.ViewData["Property"]; }

Alternatively you can just pass in the name of the template and use this code in the template

var prefix = ViewData.TemplateInfo.HtmlFieldPrefix;
if (prefix.EndsWith("InvoiceId")) { ... }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文