Home ASP.NET MVC 5 Scaffolding not creating elements for enum property
Reply: 0

ASP.NET MVC 5 Scaffolding not creating elements for enum property

user10988
1#
user10988 Published in September 19, 2018, 11:03 am

This is probably a newbie question, as I'm quite new to ASP.NET MVC 5. When I tell Visual Studio to add a View based on my ViewModel class, it completely skips properties defined like public EnumName? PropertyName { get; set; } and does not create any @Html.EditorFor calls for it.

However, if I manually add the call @Html.EditorFor(model => model.PropertyName, new { htmlAttributes = new { @class = "form-control" } }) I get exactly what I expect -- a dropdown which is empty by default. Should scaffolding not do this by itself?

My understanding is that this is supposed to be supported in the current version of ASP.NET MVC. Am I wrong about that, or am I missing something? Help or advice is greatly appreciated.

Thanks in advance!

These are the ASP.NET products installed:

ASP.NET and Web Tools 12.4.51016.0

ASP.NET Web Frameworks and Tools 2012.2 4.1.21001.0

ASP.NET Web Frameworks and Tools 2013 5.2.21010.0

Edit for sample code:

Here is a small section of the view model. There are 170 different properties, almost all of them nullable-Enum type.

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

}

Here is the complete output I get from Scaffolding. As you can see, it has completely ignored all enum properties.

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

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

@class = "control-label col-md-2" }) @Html.EditorFor(model => model.SiblingsCousinsMedicalHistoryComments, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.SiblingsCousinsMedicalHistoryComments, "", new { @class = "text-danger" })

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

@class = "control-label col-md-2" }) @Html.EditorFor(model => model.ParentsGrandparentsMedicalHistoryComments, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.ParentsGrandparentsMedicalHistoryComments, "", new { @class = "text-danger" })

    <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("Back to List", "Index")

@section Scripts { @Scripts.Render("~/bundles/jqueryval") }

But, if I add this block manually, I get exactly what I want: a drop-down which is empty by default, with my full pick list as the choices.

    <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>
share|improve this question
  • Do you have any attributes on that property, can we see code of viewmodel? – pjobs Apr 16 '15 at 3:53
  • @pjobs Thanks for responding. I've added a sample of the view model (over 170 properties in it) and the complete output. This is only one view model out of 18. :( Sorry the SO stylizer did not seem to like Razor code. – dythim Apr 18 '15 at 3:30

1 Answer 1

active oldest votes
up vote 1 down vote
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.381417 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO