John Schultz
John Schultz

Reputation: 672

How to set the selected value of an MVC5 DropDownList?

Given the following MVC5 code:

@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.

Upvotes: 2

Views: 899

Answers (1)

John Schultz
John Schultz

Reputation: 672

Here is the what Ive come up with after being reminded of the 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")

Upvotes: 2

Related Questions