预选下拉列表剃须刀页面

时间:2019-03-02 12:24:52

标签: c# razor model-view-controller razor-pages

我有以下代码用于在“剃刀”页面中填充下拉菜单 我想预先选择一个描述-可以在

中找到需要设置的“值”
  

s.User EstablishmentmentId

如何在下拉菜单中预先选择

@Html.DropDownList("drpEstablishments",
                    getEstablishments().Select(s => new SelectListItem()
                        {

                          Text = s.Description,
                          Value = s.EstablishId.ToString()
                      }),
                     new
                     {
                         @class = "dropdown form-control"
                     })

2 个答案:

答案 0 :(得分:1)

您正在使用linq为SelectListItem元素创建新的getEstablishments。创建SelectListItem()的每个实例时,您需要确定Selectedtrue还是false。只需用返回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"
                     })

答案 1 :(得分:0)

最后这样的事情奏效了

 Selected= (s.UserEstablishmentId==s.EstablishId)? true:false,