简体   繁体   English

如何设置 MVC5 DropDownList 的选定值?

[英]How to set the selected value of an MVC5 DropDownList?

Given the following MVC5 code:给定以下 MVC5 代码:

@Html.DropDownList(name: "Enhanced_Rijndael_AlgorithmID", optionLabel: null, selectList: EnhancedRijndaelAvailableHashAlgorithmList.Select(item => new SelectListItem
{
    Value = item.RecordID.ToString(),
    Text = item.HashAlgorithm,
    Selected = "select" == item.RecordID.ToString()
}), htmlAttributes: new { @class = "form-control" })

Is it possible to set the "selected value" of this list?是否可以设置此列表的“选定值”?

The only thing I can think of is somehow setting the Selected value in the SelectListItem set.我唯一能想到的是以某种方式设置SelectListItem集中的 Selected 值。

Here is the what Ive come up with after being reminded of the DropDownListFor这是我在被提醒DropDownListFor后想到的

@Html.DropDownListFor(expression: r => r.AlgorithmID, selectList: EnhancedRijndaelAvailableHashAlgorithmList.Select(item => new SelectListItem
{
    Value = item.RecordID.ToString(),
    Text = item.HashAlgorithm,
    Selected = "select" == item.RecordID.ToString()
}),
htmlAttributes: new { @class = "form-control" }, optionLabel: "Algorithm")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM