预选下拉列表剃刀页面
preselect dropdown list razor page
我使用以下代码填充我的 Razor 页面中的下拉菜单
我想预选描述 - 需要设置的 "Value" 在
中找到
s.UserEstablishmentId
如何在下拉菜单中预选此项
@Html.DropDownList("drpEstablishments",
getEstablishments().Select(s => new SelectListItem()
{
Text = s.Description,
Value = s.EstablishId.ToString()
}),
new
{
@class = "dropdown form-control"
})
您正在使用 linq 为 getEstablishments
元素创建新的 SelectListItem
。创建 SelectListItem()
的每个实例时,您需要确定 Selected
应该是 true
还是 false
。只需将 YourConditionForSelectionHere
替换为 returns a bool
的方法或 returns a 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,
我使用以下代码填充我的 Razor 页面中的下拉菜单 我想预选描述 - 需要设置的 "Value" 在
中找到s.UserEstablishmentId
如何在下拉菜单中预选此项
@Html.DropDownList("drpEstablishments",
getEstablishments().Select(s => new SelectListItem()
{
Text = s.Description,
Value = s.EstablishId.ToString()
}),
new
{
@class = "dropdown form-control"
})
您正在使用 linq 为 getEstablishments
元素创建新的 SelectListItem
。创建 SelectListItem()
的每个实例时,您需要确定 Selected
应该是 true
还是 false
。只需将 YourConditionForSelectionHere
替换为 returns a bool
的方法或 returns a 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,