问题MVC EditorFor命名的模板 [英] Problem with MVC EditorFor named template

查看:116
本文介绍了问题MVC EditorFor命名的模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有什么似乎(我反正)是一个奇怪的问题...

I have what appears (to me anyway) to be a strange problem...

我创建了SelectListItem(SelectListItem.cshtml在〜/查看/共享/ EditorTemplates文件夹)一个简单的编辑模板,例如:

I created a simple editor template for a SelectListItem (SelectListItem.cshtml in the ~/Views/Shared/EditorTemplates folder), for example:

<ul class="select-list-item cell-15 col-3 omega clearfix">
    @Html.EditorFor(c => c.Categories)
</ul>

在哪里c.Categories是一个IEnumerable

Where c.Categories is an IEnumerable

这工作得很好,但我想另外一个模板来呈现略有不同的标记集,所以我复制和重命名编辑模板,例如,CategoryIcons.cshtm和调用如下:

This worked fine, but I wanted another template to render the collection with slightly different markup, so I copied and renamed the editor template to, for example, 'CategoryIcons.cshtm' and invoked as follows:

<ul class="select-list-item cell-15 col-3 omega clearfix">
    @Html.EditorFor(c => c.Categories, "CategoryIcons")
</ul>

在短,唯一的区别是我指定名为编辑模板

In short, the only difference is I'm specifying a named editor template.

当我打开网页,我现在得到以下错误:

When I open the page, I now get the following error:

传递到字典的模型项的类型是System.Collections.Generic.List`1 [System.Web.Mvc.SelectListItem]',但本词典需要类型System.Web.Mvc的典范项目。 SelectListItem

The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[System.Web.Mvc.SelectListItem]', but this dictionary requires a model item of type 'System.Web.Mvc.SelectListItem'

该模板的模型声明,在这两个模板ID:

The template's model declaration, in both templates id:

@model System.Web.Mvc.SelectListItem

我不明白为什么默认模板工程和命名模板没有。任何帮助将是AP preciated。

I don't understand why the default template works and the named template doesn't. Any help would be appreciated.

感谢。

推荐答案

当你调用 @ Html.EditorFor(C =&GT; c.Categories)则回落为IEnumerable的默认模板。这种默认的模板是由MVC框架提供的,其行为是输出 Html.EditorFor()在枚举各个项目。这反过来又发出在单独列表中每个项目相应的编辑器模板 - 你的情况他们是 SelectListItem 的所有实例,所以在第一种情况, SelectListItem 模板用于每个项目。

When you call @Html.EditorFor(c => c.Categories) it is falling back to the default template for IEnumerable. This default template is provided by the MVC framework, and its behaviour is to output Html.EditorFor() for each item in the enumeration. That in turn emits the appropriate editor template for each item in the list individually - in your case they're all instances of SelectListItem, so in the first case, the SelectListItem template is used for each item.

在第二种情况下,通过显式设置你的 EditorFor 使用特定的编辑模板 CategoryIcons ,你告诉它使用编辑模板进行的整个枚举的,而不是允许枚举被缺省的模板,依次使用这些模板为每个枚举项目。

In the second case, by explicitly setting your EditorFor to use a particular editor template CategoryIcons, you are telling it to use that editor template for the whole enumeration, instead of allowing the enumerable to be templated by default, in turn using the template for each enumerated item.

我不知道的解决这个只是还没有最好的方法。

I'm not sure of the best way around this just yet.

一个办法是定义一个 CategoryIcons 模板,它的型号是的IEnumerable&LT的实例; CategoryIcon&GT; ,它只是foreaches的模式枚举,并执行 Html.EditorFor 为每个项目,以<一个明确的模板引用code> CategoryIcon 。然后,你把你的每个项目的编辑器模板该模板( CategoryIcon 不是 CategoryIcons )。这样,你会做称之为 @ Html.EditorFor(C =&GT; c.Categories,CategoryIcons)。

One approach would be to define a CategoryIcons template, whose model is an instance of IEnumerable<CategoryIcon>, which simply foreaches the Model enumeration, and performs Html.EditorFor for each item, with an explicit template reference of CategoryIcon. You then put your per-item editor template in that template (CategoryIcon not CategoryIcons). You would then call this by doing @Html.EditorFor(c => c.Categories, "CategoryIcons").

我要看看四周,看看是否有更好的方法来完成这件事,但我希望这也许现在是有用的。这将是巨大的,如果模板可以参数化,所以你可以写一个IEnumerable的模板,需要作为一个参数模板的名称,用于它的每一个项目。

I'm going to have a look around to see if there are better ways to get this done, but I hope this might be useful for now. It would be great if templates could be parameterized, so you could write an IEnumerable template that takes as an argument the name of the template to use for each of its items.

这篇关于问题MVC EditorFor命名的模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆