0

When I scaffold a view from my model, my Enum is ignored unless a specify a Data Context Class? Why is this?

Here is my model:

 public class customer
{
    public int customerId { get; set; }
    public Title customerTitle { get; set; }
    public string  CustomerName { get; set; }
}


public enum Title
{
    Mr, Miss, Mrs, Ms
}  

When I scaffold a create view and specify a Data Context Class I get this in my view:

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

But if I scaffold the same model again but without a Data Context the Enum isn't there at all.

This is where I do or do not specify a Data Context Class.

Adding a View

Art Vdel
  • 87
  • 6
  • Please can you provide the different code, the code with the Data Context too so that we can see the difference in the code? – Luke Apr 10 '17 at 12:03
  • What was it that you have been scaffolding? – Luke Apr 10 '17 at 12:23
  • I was scaffolding a create view for the customers model – Art Vdel Apr 10 '17 at 12:29
  • @Luke, I have added an image of where I am specifying the Data Context. – Art Vdel Apr 10 '17 at 12:37
  • Thanks, that makes things a lot more clear :) – Luke Apr 10 '17 at 12:38
  • 1
    Well, specifying a data context causes the scaffold to behave differently. That much is obvious. Apparently, one of those differences is that enum properties are included. The scaffold is the scaffold, though, so if it doesn't work the way you want it to, there's nothing you can do about that. In general, you should never rely on the scaffolding functionality, anyways. It's just training wheels. Once you know how things work, you're almost always better off just adding views and such manually. – Chris Pratt Apr 10 '17 at 13:57

0 Answers0