[英]preselect dropdown list razor page
I have the below code that I use to populate my dropdown in my Razor Page I want to preselect a description - the "Value" of that needs to be set is found in 我有以下代码,用于在我的“剃刀”页面中填充下拉菜单,我想预先选择一个说明-需要在其中设置“值”
s.UserEstablishmentId
s.UserEstablishmentId
How can I preselect this on the dropdown 如何在下拉菜单中预先选择
@Html.DropDownList("drpEstablishments",
getEstablishments().Select(s => new SelectListItem()
{
Text = s.Description,
Value = s.EstablishId.ToString()
}),
new
{
@class = "dropdown form-control"
})
You're using linq to create a new SelectListItem
for getEstablishments
element. 您正在使用linq为
getEstablishments
元素创建一个新的SelectListItem
。 When creating each instance of a SelectListItem()
you need to determine if Selected
should be true
or false
. 创建
SelectListItem()
每个实例时,您需要确定Selected
应该为true
还是false
。 Simply replace YourConditionForSelectionHere
with a method that returns a bool
or syntax that returns a bool
, shown below: 只需更换
YourConditionForSelectionHere
与返回的方法bool
或语法返回一个bool
,如下图所示:
@Html.DropDownList("drpEstablishments",
getEstablishments().Select(s => new SelectListItem()
{
Selected = (YourConditionForSelectionHere),
Text = s.Description,
Value = s.EstablishId.ToString()
}),
new
{
@class = "dropdown form-control"
})
最后像这样的事情
Selected= (s.UserEstablishmentId==s.EstablishId)? true:false,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.