通用类型的ASP.NET MVC显示模板 [英] ASP.NET MVC Display Template for Generic Type
问题描述
我试图将模型ListModel用作通用列表模型.我想在页面上输入
I am trying to use the model ListModel as a generic list model. I would like to enter on the page
@Html.DisplayForModel()
但是,MVC无法正确找到模板文件"ListModel.cshtml".对于通用模型,它必须以不同的方式工作.为了正确定位模板文件,我应如何命名?
However the MVC is not correctly finding the templated file "ListModel.cshtml". It must work differently for generic models. What should I name the templated file in order for it to correctly be located?
public class ListModel<T>
{
public IEnumerable<T> Models { get; set; }
public string NextPage { get; set; }
}
我希望它会寻找Shared/DisplayTemplates/ListModel.ascx
,但不会.有人知道吗?
I would expect it to look for Shared/DisplayTemplates/ListModel.ascx
but it doesn't. Does anyone know?
我确实通过简单地删除通用参数来解决了这个问题.我想知道您是否仍然可以使用通用文件名.
I did end up solving this by simply removing the generic parameter like so. I do want to know if you can still have a generic file name though.
public class ListModel
{
public IEnumerable Models {get;set;}
public string NextPage {get;set;}
}
推荐答案
作为一种解决方法,您可以用UIHint
属性修饰ListModel<T>
类以强制其使用您想要的模板吗?
As a workaround, could you decorate the ListModel<T>
class with the UIHint
attribute to force it to use the template you want?
例如,
[UIHint("ListModel")]
public class ListModel<T>
{
...
这篇关于通用类型的ASP.NET MVC显示模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!