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

问题描述 投票:1回答:1

这可能是一个新手问题,因为我是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工具12.4.51016.0

ASP.NET Web框架和工具2012.2 4.1.21001.0

ASP.NET Web框架和工具2013 5.2.21010.0

编辑示例代码:

这里是视图模型的一小部分。有170个不同的属性,几乎所有属性都为nullable-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; }
  }

这里是我从脚手架获得的complete输出。如您所见,它已经完全忽略了所有枚举属性。

@model QmeSurveyApp.ViewModels.MedicalHistoryViewModel

@{
    ViewBag.Title = "EditMedicalHistory"; }

<h2>EditMedicalHistory</h2>


@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" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.SleepDisturbanceAttributedToComments, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.SleepDisturbanceAttributedToComments, "", new { @class = "text-danger" })
            </div>
        </div>

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

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

        <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> }

<div>
    @Html.ActionLink("Back to List", "Index") </div>

@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>
asp.net-mvc enums scaffolding
1个回答
1
投票

您没有提到它,但是我猜您没有使用实体框架。

© www.soinside.com 2019 - 2024. All rights reserved.