格式化@HtmlDropDownListFor使用htmlAttributes在引用css类时不起作用

时间:2015-04-02 18:02:38

标签: razor-2 asp.net-mvc-5.1

这很奇怪,或者我只是不明白这是如何工作的。我有以下代码:

    <div class="form-group">
        @Html.LabelFor(model => model.Department, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownListFor(dep=>dep.DepartmentId,new SelectList(ViewBag.Departments,"Id","Name"), new {htmlAttributes = new {@class="mySelect"}})
            @Html.ValidationMessageFor(model => model.Department, "", new { @class = "text-danger" })
        </div>
    </div>

样式表条目:

.mySelect {
    width: 5000px;
    border: groove;
}

以上组合不会格式化我的下拉列表,但如果我执行直接格式化,则可以使用:

    <div class="form-group">
        @Html.LabelFor(model => model.Department, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownListFor(dep=>dep.DepartmentId,new SelectList(ViewBag.Departments,"Id","Name"), new {@style = "width:500px;border:groove;"})
            @Html.ValidationMessageFor(model => model.Department, "", new { @class = "text-danger" })
        </div>
    </div>

另外,如果我将@style添加到htmlAttribute部分,它不起作用,也不会用@class =&#34替换@style位; mySelect&#34; ...

谢谢!

1 个答案:

答案 0 :(得分:0)

在没有@class的情况下使用直接htmlAttributes

@Html.DropDownListFor(dep=>dep.DepartmentId,new SelectList(ViewBag.Departments,"Id","Name"), new { @class="mySelect" })