I've created an EditorTemplate for string fields that implements bootstrap as follows:
@using MyProject
@model object
<div class="form-group">
@Html.LabelFor(m => m, new { @class = "col-md-3 control-label" })
<div class="col-md-9">
@Html.TextBox(
"",
ViewData.TemplateInfo.FormattedModelValue,
htmlAttributes)
@Html.ValidationMessageFor(m => m, null, new { @class = "help-block" })
</div>
</div>
And I can call this simply like this:
@Html.EditorFor(model => model.FirstName,"BootstrapString")
My Question: How would I do this for a DropDownList so that I can merely call @Html.EditorFor as follows:
@Html.EditorFor(model => model.CategoryId,new SelectList(ViewBag.Categories, "ID", "CategoryName"))
So it's basically a Generic DropDownList with Twitter Bootstrap styling.
Option 1
Create an
EditorTemplate
namedBootstrapSelect.cshtml
and in the view
but this means you would alway need to assign `ViewBag.Items in the controller
Option 2
Modify the
EditorTemplate
to accept additional ViewDataand in the view pass the
SelectList
in theadditionalViewData
parameterthis is better in that you don't need to rely on ViewBag. For example, if you had a view model with a property
public SelectList CategoryItems { get; set; }
then you could useOption 3
Create your own helper utilizing the built-in helper methods
and in the view