Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在MVC4中为枚举创建默认编辑器模板?_C#_Asp.net Mvc_Templates_Asp.net Mvc 4_Enums - Fatal编程技术网

C# 如何在MVC4中为枚举创建默认编辑器模板?

C# 如何在MVC4中为枚举创建默认编辑器模板?,c#,asp.net-mvc,templates,asp.net-mvc-4,enums,C#,Asp.net Mvc,Templates,Asp.net Mvc 4,Enums,我们知道,如果我们为一个基类型定义一个模板,如果没有使用任何其他模板来覆盖它,那么该模板也可以用于派生类型 由于我们无法继承枚举,也不认为枚举是从枚举继承的,因此Views\Shared\EditorTemplates中的Enum.cshtml模板对于对象的不同自定义枚举属性都不会处于活动状态,例如: public enum Role { Admin, User, Guest } 我已经看到了一些关于ASP的答案,但是我想知道MVC4在这个问题上是否有一些改进 另外,我

我们知道,如果我们为一个基类型定义一个模板,如果没有使用任何其他模板来覆盖它,那么该模板也可以用于派生类型

由于我们无法继承枚举,也不认为枚举是从枚举继承的,因此Views\Shared\EditorTemplates中的Enum.cshtml模板对于对象的不同自定义枚举属性都不会处于活动状态,例如:

public enum Role
{
    Admin,
    User,
    Guest
}
我已经看到了一些关于ASP的答案,但是我想知道MVC4在这个问题上是否有一些改进

另外,我的意思是不使用任何明确的模板属性,比如@Html.EditorFormodel=>model.Role、Enum或[uihintnum]


PPS。我是MVC的新手,所以我会很感激你的简单回答。

K.斯科特·艾伦对此有很好的理解。

K.斯科特·艾伦对此有很好的理解。

在MVC 5中,你可以向视图->共享->编辑器添加一个包含以下代码的模板:

@model Enum
@{
    var optionLabel = ViewData["optionLabel"] as string;
    var htmlAttributes = ViewData["htmlAttributes"];
}
@Html.EnumDropDownListFor(m => m, optionLabel, htmlAttributes)
用法示例:

@Html.EditorFor(model => model.PropertyType, 
                new { 
                       htmlAttributes = new { @class = "form-control" },
                       optionLabel = "Select" 
                    })
在MVC 4中,您没有EnumDropDownListFor扩展,但您可以使用自己的扩展,我以前是这样做的:

public static MvcHtmlString DropDownListFor<TModel, TEnum>
   (this HtmlHelper<TModel> htmlHelper, 
    Expression<Func<TModel, TEnum>> expression, 
    string optionLabel = null, object htmlAttributes = null)
{
    //This code is based on the blog - it's finding out if it nullable or not
    Type metaDataModelType = ModelMetadata
       .FromLambdaExpression(expression, htmlHelper.ViewData).ModelType;
    Type enumType = Nullable.GetUnderlyingType(metaDataModelType) ?? metaDataModelType;

    if (!enumType.IsEnum)
        throw new ArgumentException("TEnum must be an enumerated type");  


    IEnumerable<SelectListItem> items = Enum.GetValues(enumType).Cast<TEnum>()
       .Select(e => new SelectListItem
               {
                  Text = e.GetDisplayName(),
                  Value = e.ToString(),
                  Selected = e.Equals(ModelMetadata
                     .FromLambdaExpression(expression, htmlHelper.ViewData).Model)
               });

    return htmlHelper.DropDownListFor(
        expression,
        items,
        optionLabel,
        htmlAttributes
        );
}
参考资料:


在MVC 5中,您可以将模板添加到包含以下代码的视图->共享->编辑模板:

@model Enum
@{
    var optionLabel = ViewData["optionLabel"] as string;
    var htmlAttributes = ViewData["htmlAttributes"];
}
@Html.EnumDropDownListFor(m => m, optionLabel, htmlAttributes)
用法示例:

@Html.EditorFor(model => model.PropertyType, 
                new { 
                       htmlAttributes = new { @class = "form-control" },
                       optionLabel = "Select" 
                    })
在MVC 4中,您没有EnumDropDownListFor扩展,但您可以使用自己的扩展,我以前是这样做的:

public static MvcHtmlString DropDownListFor<TModel, TEnum>
   (this HtmlHelper<TModel> htmlHelper, 
    Expression<Func<TModel, TEnum>> expression, 
    string optionLabel = null, object htmlAttributes = null)
{
    //This code is based on the blog - it's finding out if it nullable or not
    Type metaDataModelType = ModelMetadata
       .FromLambdaExpression(expression, htmlHelper.ViewData).ModelType;
    Type enumType = Nullable.GetUnderlyingType(metaDataModelType) ?? metaDataModelType;

    if (!enumType.IsEnum)
        throw new ArgumentException("TEnum must be an enumerated type");  


    IEnumerable<SelectListItem> items = Enum.GetValues(enumType).Cast<TEnum>()
       .Select(e => new SelectListItem
               {
                  Text = e.GetDisplayName(),
                  Value = e.ToString(),
                  Selected = e.Equals(ModelMetadata
                     .FromLambdaExpression(expression, htmlHelper.ViewData).Model)
               });

    return htmlHelper.DropDownListFor(
        expression,
        items,
        optionLabel,
        htmlAttributes
        );
}
参考资料:

还有,;有:

@Html.EnumDropDownListFor(model => model.Role, new { @class = ".." })
当然,角色是枚举:

;有:

@Html.EnumDropDownListFor(model => model.Role, new { @class = ".." })

当然,作为枚举人的角色:

我有点搞不懂你要什么。因此,简单地为Enum定义一个编辑器模板,即Enum.cshtml,是不够的?apriori,是不够的…您能给出一个从您的Enum生成所需html的示例吗?我有点不明白您想要什么。因此,简单地为Enum定义一个编辑器模板,即Enum.cshtml,是不够的?apriori,是不够的…您能给出一个从您的Enum生成所需html的示例吗?好文章。但需要包括可为空的枚举模型typesGood文章。但需要包括可为空的枚举模型类型