问题描述
我创建它目前使用EditorFor到gennerate了很多意见MVC应用程序。整体看法是基本上只是一个EditorForModel,它的伟大工程。不过,我已经达到了一个小问题,我似乎无法找到一个解决方案,而且重要的是,它的工作原理我需要它的方式,在尝试呈现EditorFor接口时。该绑定和一切都像一个已经照顾,但问题是,EditorFor发现它是一个接口,默认为对象的模板。我需要它看界面,看看它是否能找到该名称的模板,如果不能,我需要它看起来槽的所有接口present,看它是否与其中之一匹配。为了解释得更简单看看下面这个例子:
I'm creating a MVC-application which currently uses EditorFor to gennerate a lot of views. The whole view is basically just an EditorForModel, and it works great. However, I've reached one small problem, which I can't seem to find a solution for, and it is important that it works the way I need it to, and that is when trying to render EditorFor an interface. The bindings and everything like that's been taken care of, but the problem is that the EditorFor sees that it's an interface, and defaults to the "Object" template. I need it to look at the interface and see if it can find a template with that name, and if it can't, I need it to look trough all the interfaces present to see if it matches any of them. To explain it more simply look at this example:
接口:
public interface IAppProvider
{
string Name { get; set; }
}
public interface IAppMusicProvider : IAppProvider
{
int GetPlaylistCount();
} // Yeah, I know, this interface is not smart, but it's only for show.
如果我现在创建一个模型=IAppMusicProvider视图,并执行 Html.EditorForModel()
,我需要它来找到〜视图\\共享\\ EditorTemplates \\ IAppProvider.cshtml-template。有没有简单的方法,我可以做到这一点?
If I now create a View with model = "IAppMusicProvider", and run Html.EditorForModel()
, I need it to find the "~Views\Shared\EditorTemplates\IAppProvider.cshtml"-template. Is there any simple way I can achieve this?
推荐答案
你有没有尝试使用 [TemplateHint]
属性?
Did you try using the [TemplateHint]
attribute?
这篇关于它将覆盖ASP.NET MVC 3 RC默认EditorFor模板选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!