【发布时间】:2015-04-15 02:40:20
【问题描述】:
这可能是一个新手问题,因为我对 ASP.NET MVC 5 还很陌生。当我告诉 Visual Studio 添加基于我的 ViewModel 类的视图时,它会完全跳过像 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 种不同的属性,几乎都是可以为空的枚举类型。
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"; }
<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>
【问题讨论】:
-
你有那个属性的属性,我们可以看看viewmodel的代码吗?
-
@pjobs 感谢您的回复。我添加了一个视图模型示例(其中包含 170 多个属性)和 complete 输出。这只是 18 个视图模型中的一个。:( 抱歉,SO 样式器似乎不喜欢 Razor 代码。
标签: asp.net-mvc enums scaffolding