ASP.NET MVC 5脚手架没有为枚举属性创建元素

时间:2015-04-15 02:40:20

标签: asp.net-mvc enums scaffolding

这可能是一个新手问题,因为我对ASP.NET MVC 5很陌生。当我告诉Visual Studio基于我的ViewModel类添加一个View时,它完全跳过像public EnumName? PropertyName { get; set; }这样定义的属性。不会为它创建任何@ Html.EditorFor调用。

但是,如果我手动添加呼叫@Html.EditorFor(model => model.PropertyName, new { htmlAttributes = new { @class = "form-control" } }),我会得到我所期望的 - 默认为空的下拉列表。脚手架本身不应该这样做吗?

我的理解是,应该在当前版本的ASP.NET MVC中支持它。我错了,还是我错过了什么?非常感谢帮助或建议。

提前致谢!

这些是安装的ASP.NET产品:

ASP.NET和Web Tools 12.4.51016.0

ASP.NET Web框架和工具2012.2 4.1.21001.0

ASP.NET Web框架和工具2013 5.2.21010.0

编辑示例代码:

以下是视图模型的一小部分。有170种不同的属性,几乎所有属性都是可空的Enum类型。

 public partial class MedicalHistoryViewModel
{
    public YesNo? Cancer { get; set; }

    public MedicalHistoryDiagnosed? CancerDiagnosed { get; set; }

    public YesNoUnsure? CancerIndustrialInOrigin { get; set; }

    public YesNo? Diabetes { get; set; }

    public MedicalHistoryDiagnosed? DiabetesDiagnosed { get; set; }

    public YesNoUnsure? DiabetesIndustrialInOrigin { get; set; }

    public YesNo? HeartDisease { get; set; }

    //...

    [Display(Name = @"Do you attribute the sleep disturbance to pain, anxiety and/or depression, or to other factors?")]
    [DataType(DataType.MultilineText)]
    public string SleepDisturbanceAttributedToComments { get; set; }

    [Display(Name = @"Other (please specify)")]
    [DataType(DataType.MultilineText)]
    public string ParentsGrandparentsMedicalHistoryComments { get; set; }

}

以下是我从Scaffolding获得的完整输出。如您所见,它完全忽略了所有枚举属性。

  

@model QmeSurveyApp.ViewModels.MedicalHistoryViewModel

     

@ {       ViewBag.Title =“EditMedicalHistory”; }

     

EditMedicalHistory

     

@using(Html.BeginForm()){       @ Html.AntiForgeryToken()

<div class="form-horizontal">
    <h4>MedicalHistoryViewModel</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(model => model.SleepDisturbanceAttributedToComments, htmlAttributes: new {
     

@class =“control-label col-md-2”})                                  @ Html.EditorFor(model =&gt; model.SleepDisturbanceAttributedToComments,new {htmlAttributes = new   {@class =“form-control”}})                   @ Html.ValidationMessageFor(model =&gt; model.SleepDisturbanceAttributedToComments,“”,new {@class =   “文字危险”})                          

    <div class="form-group">
        @Html.LabelFor(model => model.SiblingsCousinsMedicalHistoryComments, htmlAttributes: new {
     

@class =“control-label col-md-2”})                                  @ Html.EditorFor(model =&gt; model.SiblingsCousinsMedicalHistoryComments,new {htmlAttributes =   新{@class =“form-control”}})                   @ Html.ValidationMessageFor(model =&gt; model.SiblingsCousinsMedicalHistoryComments,“”,new {@class =   “文字危险”})                          

    <div class="form-group">
        @Html.LabelFor(model => model.ParentsGrandparentsMedicalHistoryComments, htmlAttributes: new {
     

@class =“control-label col-md-2”})                                  @ Html.EditorFor(model =&gt; model.ParentsGrandparentsMedicalHistoryComments,new {htmlAttributes   = new {@class =“form-control”}})                   @ Html.ValidationMessageFor(model =&gt; model.ParentsGrandparentsMedicalHistoryComments,“”,new {@class =   “文字危险”})                          

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Save" class="btn btn-default" />
        </div>
    </div>
</div> }
            @ Html.ActionLink(“返回列表”,“索引”)      

@section Scripts {       @ Scripts.Render(“〜/ bundles / jqueryval”)}

但是,如果我手动添加此块,我会得到我想要的内容:默认为空的下拉列表,我的完整选择列表作为选项。

    <div class="form-group">
        @Html.LabelFor(model => model.Cancer, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Cancer, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Cancer, "", new { @class = "text-danger" })
        </div>
    </div>

1 个答案:

答案 0 :(得分:1)

你没有提到它,但我猜你没有使用Entity Framework。

我在没有EF工作的MVC项目中遇到了类似的情况。我有一个POCO,其属性是枚举,并且脚手架引擎完全跳过了它。我甚至尝试使用我自己的CodeTemplates覆盖T4模板,当我注意到ModelMetadata.Properties集合甚至不包含我的枚举属性时。

我终于通过向项目中添加一个空的Code First Entity Data模型来实现它。这样做会将Data context class文本框添加到Add View脚手架项目,结果脚手架视图现在包含我的枚举属性。这对我来说似乎是个错误。