Firstly, I am new to MVC and ASP.NET, so I apologize if I am missing something simple.
I am working on the first MVC 5 application for code, for brevity I will say that I have two models, such as:
public class Platform { [Key] public int Id { get; set; } [Required(ErrorMessage = "You must select a manufacturer")] [DisplayName("Manufacturer")] public int ManufacturerId { get; set; } public virtual Manufacturer Manufacturer { get; set; } [Required(ErrorMessage="You must enter a name for the platform")] [StringLength(50, ErrorMessage="Reduce length to 50 characters or less")] public string Name { get; set; } }
and
public class Manufacturer { [Key] public int Id { get; set; } [Required(ErrorMessage="You must enter a name for the manufacturer")] [StringLength(50, ErrorMessage="Reduce length to 50 characters or less")] public string Name { get; set; } public virtual ICollection<Platform> Platforms { get; set; } }
When creating the "MVC 5 Controller with views using the Entity Framework" for these models, all CRUD actions work correctly. Form elements, on the other hand, are unformatted, lacking the form-control class that Bootstrap wants.
I can get around this for each type by adding an EditorTemplate to it with something like @Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue, new { @class = "form-control" }) , but certainly scaffolding for Bootstrap MVC 5 project should already emit valid code? Looking at the various MVC 5 tutorials, they seem to work correctly with the right style, so I'm a bit confused about what I'm doing wrong.
For reference, the output in the Create.cshtml file for each model (tied to important form elements for short):
<div class="form-group"> @Html.LabelFor(model => model.ManufacturerId, "ManufacturerId", new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.DropDownList("ManufacturerId", String.Empty) @Html.ValidationMessageFor(model => model.ManufacturerId) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.Name, new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Name) @Html.ValidationMessageFor(model => model.Name) </div> </div>
and
<div class="form-group"> @Html.LabelFor(model => model.Name, new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Name) @Html.ValidationMessageFor(model => model.Name) </div> </div>
Why is ManufacturerId indicated as shortcut text for the drop-down list when DisplayName set in the model?
Why did you choose using DropDownList() instead of the strongly typed DropDownListFor() ?
Thanks in advance.