通用类型的ASP.NET MVC显示模板 [英] ASP.NET MVC Display Template for Generic Type

查看:122
本文介绍了通用类型的ASP.NET MVC显示模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将模型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屋!

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