【问题标题】:Formatting @HtmlDropDownListFor using htmlAttributes not working when referencing css class引用css类时使用htmlAttributes格式化@HtmlDropDownListFor不起作用
【发布时间】:2015-04-02 18:02:37
【问题描述】:

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

    <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="mySelect" 替换@style 位也不起作用...

谢谢!

【问题讨论】:

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


    【解决方案1】:

    直接使用@class,而不使用htmlAttributes

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-03
      • 2023-03-04
      • 2016-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多