@ Html.DropDownList宽度 [英] @Html.DropDownList width
问题描述
问:如何为@ Html.DropDownList宽度(而不是CSS)?
@ Html.DropDownList(ListId的String.Empty,新{风格=宽度:250像素;})!@ *没有去* @
DropDownList的助手必须是的IEnumerable< SelectListItem>
。您传递一个字符串(空单更precise)。因此,为了使用这个帮助你必须尊重其签名:
@ Html.DropDownList(
ListId
Enumerable.Empty< SelectListItem>()
新{风格=宽度:250像素; }
)
显然产生一个空的下拉列表中很少使用。你可能有一个视图模型(你应该的方式)要使用绑定到:
@ Html.DropDownList(
ListId
Model.MyList,
新{风格=宽度:250像素; }
)
当然,因为你有一个视图模型,你应该preFER使用 DropDownListFor
助手:
@ Html.DropDownListFor(
X => x.ListId,
Model.MyList,
新{风格=宽度:250像素; }
)
最后,以避免与风格弄乱你的HTML,你应该使用一个外部CSS:
@ Html.DropDownListFor(
X => x.ListId,
Model.MyList,
新{@class =mycombo}
)
其中外部CSS,你会定义 .mycombo
规则:
.mycombo {
宽度:250像素;
}
现在你有什么,我认为正确的code。
Q: How to set width for @Html.DropDownList (and not in css)?
@Html.DropDownList("ListId", String.Empty, new {style="width: 250px;"}) @* no go!*@
The second argument of the DropDownList helper must be an IEnumerable<SelectListItem>
. You are passing a string (an empty one to be more precise). So in order to use this helper you will have to respect its signature:
@Html.DropDownList(
"ListId",
Enumerable.Empty<SelectListItem>(),
new { style = "width: 250px;" }
)
Obviously generating an empty dropdown list is of little use. You probably have a view model (you should by the way) that you want to use to bind to:
@Html.DropDownList(
"ListId",
Model.MyList,
new { style = "width: 250px;" }
)
and of course because you have a view model you should prefer to use the DropDownListFor
helper:
@Html.DropDownListFor(
x => x.ListId,
Model.MyList,
new { style = "width: 250px;" }
)
and finally to avoid cluttering your HTML with styles you should use an external CSS:
@Html.DropDownListFor(
x => x.ListId,
Model.MyList,
new { @class = "mycombo" }
)
where in your external CSS you would define the .mycombo
rule:
.mycombo {
width: 250px;
}
Now you have what I consider a proper code.
这篇关于@ Html.DropDownList宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!