如何在MVC 4中为枚举创建默认编辑器模板? [英] How to make a default editor template for enums in MVC 4?

查看:171
本文介绍了如何在MVC 4中为枚举创建默认编辑器模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们知道,如果我们为基类型定义一个模板,该模板也可以用于派生类型(如果没有其他模板用于覆盖它)。

We know that if we define a template for a base type, that template could serve also for the derived types (if any other template was not used to override it).

由于我们无法继承枚举,也不能枚举被继承自枚举,所以 Views\Shared\EditorTemplates Enum.cshtml 模板$ c>不会对对象的不同自定义枚举属性生效,如下所示:

As we can't inherit an Enum, nor enums are considered inherited from the Enum, so neither the Enum.cshtml template in the Views\Shared\EditorTemplates will not be active for different custom enum properties of the objects, like this one:

public enum Role
{
    Admin,
    User,
    Guest
}

我已经看到ASP的这个主题的一些答案,但我想知道如果在 MVC 4 有这方面有一些改进?

I already saw some answers on this topic for ASP in general, but I am wondering if in MVC 4 there are some improvements on this subject?

PS。我的意思是没有使用任何明确的模板归因(如 @ Html.EditorFor(model => model.Role,Enum) [UIHint(Enum)]

PS. I mean without use any explicit template attribution (like @Html.EditorFor(model => model.Role, "Enum") or [UIHint("Enum")])

PPS。我是MVC的新手,所以我会感谢你的简单答案。

PPS. I am novice in MVC, so I'll appreciate your simple answers.

推荐答案

Scott Allen有一个很好的文章关于这个。

K. Scott Allen has a nice article about this.

这篇关于如何在MVC 4中为枚举创建默认编辑器模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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