如何将引导样式应用于ASP.NET-MVC5应用程序中的dropdownlistFor和EditorFor

时间:2015-07-06 15:23:34

标签: css twitter-bootstrap razor asp.net-mvc-5

我在ASP.NET-MVC5应用程序中有dropdownListFor,主要是我使用Bootstrap样式但是很难做样式dropdownList。

<div class="form-group">
        @Html.LabelFor(model => model.Gender, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownListFor(model => model.Gender, new SelectListItem[]{ 
                                                                new SelectListItem{Value = "Male", Text="Male"},
                                                                new SelectListItem{Value = "Female", Text="Female"}
                                                                }, new { htmlAttributes = new { id = "Gender", @class = "form-control" } }

            @Html.ValidationMessageFor(model => model.Gender, "", new { @class = "text-danger" })
        </div>
    </div>

我也有整数值的EditorFor,字段显示textfild最右侧的小按钮,可以用来增加和减少数字,也没有合适的样式,一个来自bootstrap

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

enter image description here

1 个答案:

答案 0 :(得分:3)

问题在于您将new { htmlAttributes = new { ... } }传递给Html.DropDownListFor。相反,只需通过new { ... }。前者仅对Html.EditorFor是必需的,因为您传入那里的参数是additionalViewData,而对于Html.DropDownListFor,它是htmlAttributes { {1}}。