None
None

Reputation: 5670

Html.DropDownListFor selected value is not setting as selected

My code to print a dropdown is like this

        @Html.DropDownListFor(n => n.LevelTwo,
        new SelectList(""),"My selected value", new { @class = "custom-select" })

This gives me an output like this

<select class="custom-select"   id="LevelTwo" name="LevelTwo">
<option value="">My selected value</option>
But I want it to be something like this
    <select class="custom-select"   id="LevelTwo" name="LevelTwo">
<option selected="selected" value="My selected value">My selected value</option>

How can I achieve this? Note: Other elements of the dropdown will be added later using Ajax. Which won't disrupt above explained usecase

Upvotes: 0

Views: 53

Answers (1)

Izzy
Izzy

Reputation: 6866

Change your code to:

@Html.DropDownListFor(n=> n.LevelTwo, new List<SelectListItem> { new SelectListItem{ Text= "My selected value", Value= "My selected value" }}, new { @class = "custom-select" })

Upvotes: 1

Related Questions