微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

css – 如何将类放入Html.Dropdownlist中?

我有这个奇怪的问题,但它困扰了我很多.我有这个css类:

class="form-control"

我需要在我的DropDown中使用它:

@Html.DropDownList("destination",((SelectList)ViewBag.DropList).Select(t => new SelectListItem() { Text = t.Text,Value = t.Text,Selected = (t.Text == ViewBag.Destination)}),(SelectList)ViewBag.DropList)

解决方法

您似乎正在尝试在DropDownList帮助程序中设置SelectList的SelectedValue,这是不可能的.

您必须在创建SelectList时设置SelectedValue属性.请参阅以下示例.

ViewBag.DropList = new SelectList(new[]{
    new SelectListItem{ Text="one",Value="1"},new SelectListItem{ Text="two",Value="2"},new SelectListItem{ Text="three",Value="3"}
    },"Value","Text",2);

在上面的代码中,我在初始化SelectList时将2设置为所选项.之后,您可以传递SelectList和HTML属性,如下所示.

@Html.DropDownList("destination",(SelectList)ViewBag.DropList,new { @class = "form-control" })

谢谢!

解:

@Html.DropDownList("destination",((SelectList)ViewBag.DropList).Select(t => new SelectListItem() {Text = t.Text,new{@class="form-control"})

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。