加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 用于枚举下拉列表的自定义MVC模板

发布时间:2020-12-16 09:38:22 所属栏目:asp.Net 来源:网络整理
导读:我已经为EditorFor和DisplayFor帮助方法创建了几个MVC模板,以便按照我想要的方式使用Twitter Bootstrap框架来设置样式.我现在有一个工作解决方案,我需要的所有位,但我想概括我设置的一个部分,以显示状态列表.我有一个州枚举(包含所有美国州的列表),我在下拉
我已经为EditorFor和DisplayFor帮助方法创建了几个MVC模板,以便按照我想要的方式使用Twitter Bootstrap框架来设置样式.我现在有一个工作解决方案,我需要的所有位,但我想概括我设置的一个部分,以显示状态列表.我有一个州枚举(包含所有美国州的列表),我在下拉列表中显示用户地址.我使用[DataType]属性来让MVC使用我的State.cshtml模板.

[Required]
[Display(Name = "State")]
[DataType("State")]
public State State { get; set; }

所以它工作得很好,但我想改变它,以便我可以做一些像DataType(“Enum”)或其他一些方法来一般地为所有枚举命中这个模板.

模板看起来像这样:

@using System
@using System.Linq
@using Beno.Web.Helpers
@using TC.Util

@model Beno.Model.Enums.State

<div class="control-group">
    @Html.LabelFor(m => m,new {@class = "control-label{0}".ApplyFormat(ViewData.ModelMetadata.IsRequired ? " required" : "")})
    <div class="controls">
        <div class="input-append">
            @Html.EnumDropDownListFor(m => m)
            <span class="add-on">@(new MvcHtmlString("{0}".ApplyFormat(ViewData.ModelMetadata.IsRequired ? " <i class="icon-star"></i>" : "")))</span>
        </div>
        @Html.ValidationMessageFor(m => m,null,new {@class = "help-inline"})
    </div>
</div>

EnumDropDownListFor是我之前发布的辅助方法,它通常用于任何枚举.我不知道的是我如何更改此模板以将通用枚举作为模型对象?

更新:为了完整性,我包括EnumDropDownListFor方法的列表:

public static MvcHtmlString EnumDropDownListFor<TModel,TProperty>(this HtmlHelper<TModel> htmlHelper,Expression<Func<TModel,TProperty>> expression,object htmlAttributes = null) where TProperty : struct,IConvertible
{
    if (!typeof(TProperty).IsEnum)
        throw new ArgumentException("TProperty must be an enumerated type");

    var selectedValue = ModelMetadata.FromLambdaExpression(expression,htmlHelper.ViewData).Model.ToString();
    var selectList = new SelectList(from value in EnumHelper.GetValues<TProperty>()
                                    select new SelectListItem
                                                {
                                                    Text = value.ToDescriptionString(),Value = value.ToString()
                                                },"Value","Text",selectedValue);

    return htmlHelper.DropDownListFor(expression,selectList,htmlAttributes);
}

将模型类型更改为Enum会在调用helper方法的行上产生以下错误:

CS0453: The type 'System.Enum' must be a non-nullable value type in order to use it as parameter 'TProperty' in the generic type or method 'Beno.Web.Helpers.ControlHelper.EnumDropDownListFor<TModel,TProperty>(System.Web.Mvc.HtmlHelper<TModel>,System.Linq.Expressions.Expression<System.Func<TModel,TProperty>>,object)'

然后,如果我删除检查,如果TProperty是枚举和结构约束,我得到一个编译错误的行,我试图获取枚举值:

System.ArgumentException: Type 'Enum' is not an enum

我想知道我不能做我在这里尝试的事情.

解决方法

你可以创建一个EditorTemplate Enum.cshtml

你所要做的就是改变这一行:

@model Beno.Model.Enums.State

为了这 :

@model System.Enum

然后,您就可以使用任何枚举.

catch:引擎无法推断项的基类,因此,TestEnum不会被赋予Enum模板,因此您必须明确地调用它:

@Html.EditorFor(model => model.EnumValue,"Enum")

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读