【问题标题】:how to apply bootstrap style to dropdownlistFor and EditorFor in ASP.NET-MVC5 app如何在 ASP.NET-MVC5 应用程序中将引导样式应用于 dropdownlistFor 和 EditorFor
【发布时间】:2015-09-23 19:48:40
【问题描述】:

我在 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>

【问题讨论】:

  • 您希望将哪种样式应用于您的下拉菜单和 EditorFor,EditorFor 在 MVC 中有模板,可以用编辑器模板覆盖,或者您可以使用 TextBoxFor 覆盖样式。跨度>

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


【解决方案1】:

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

【讨论】:

  • 非常感谢 Chris 的 dropdowListFor,它可以工作,但我仍在为 Html.EditorFor 整数值而苦苦挣扎。我在我的问题中上传了图片,显示了错误是如何出现的
  • 使用 EditorFor 传递 htmlAttributes 仅在 1) 您使用默认编辑器模板和 2) 您使用 MVC 5.1+ 时有效。如果您使用的是自定义模板,则需要在此处遵循我的指南:cpratt.co/html-editorfor-and-htmlattributes。如果您使用的是较低版本的 MVC,则需要升级或按照我的指南进行操作。
  • 不会想到它需要htmlAttributes = 部分!不错的答案!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-04-01
  • 2012-07-13
  • 2014-10-14
  • 1970-01-01
  • 2019-04-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多