czuroski
czuroski

Reputation: 4332

MVC6 Taghelper Select Control with Selected Item in indexed model

I am trying to set the selected value of a select control based on a model in an indexed situation. I have the following code -

<select asp-for="Items.Details[i].One"
    asp-items="Utility.DropdownListItems(date, Model.Items.Details[i].One)">
</select>

I am setting the selected item in the Utility that returns the SelectList, and would expect that to set the selected item of the drop down, but that doesn't work. If I inspect the option elements, none have the selected tag. I know there was an issue with indexed data in previous iterations of Mvc HtmlHelpers. Does anyone know if this was resolved in MVC6?

Upvotes: 0

Views: 1388

Answers (1)

Doug
Doug

Reputation: 101

For both HTML and tag helpers, generated <option> elements are selected based on the expression value (Item.Details[i].One in your case). IsSelected from the select list matters only if the expression value is null i.e. in the Create case.

We have corrected a number of issues related to indexed data in MVC 6. Please file an issue at https://github.com/aspnet/Mvc/issues if something is still not working.

Upvotes: 1

Related Questions