0

I have a details-view where a varying number of sites are displayed in textboxes. Next to each site the country should be shown in a disabled dropdown. My problem is that the dropdown does not select the right value. Instead it always selects the first item. Some debugging showed that "SelectedCountries[i]" is holding the correct value for each dropdown but somehow this value does not affect the selection at all.

The dropdown's names are "SelectedCountries[i]" and I've read it is bad to use the same name for the dropdown and for the field in the model. However, changing it didn't solve the problem.

        <div id="allSitesAndCountries">
            @for (int i = 0; i < Model.SelectedSites.Count; i++)
            {
               @Html.TextBoxFor(m => m.SelectedSites[i], new { disabled = "disabled", })
               @Html.DropDownListFor(m => m.SelectedCountries[i], Model.AllCountriesSelectListItems, new{disabled = "disabled"})
            }
        </div>      

What am I doing wrong here? Thanks in advance.

EDIT: I got it working by creating the SelectList directly in the view and adding the 4th parameter:

        <div id="allSitesAndCountries">
            @for (int i = 0; i < Model.SelectedSites.Count; i++)
            {
                <div class="editor-label">
                    @Html.TextBoxFor(m => m.SelectedSites[i], new { disabled = "disabled"})
                    @Html.DropDownListFor(m => m.SelectedCountries[i], new SelectList(Model.AllCountries, "Id", "Name", Model.SelectedCountries[i]), new{disabled = "disabled"})                        
                </div>
            }
        </div>      
oHoodie
  • 209
  • 4
  • 13
  • what is the type of SelectedCountries, Is an Array[Object], Array[string] or Array[int]. – Dawood Awan Feb 23 '15 at 15:24
  • It is a List. Does it have to be an array? For the sites, which are strings, the list works... – oHoodie Feb 23 '15 at 15:26
  • No it doesn't have to be an array, – Dawood Awan Feb 23 '15 at 15:27
  • @Html.DropDownList("SelectedCountries", Model.AllCountriesSelectListItems, new{disabled = "disabled"}) , can you use like this ,I hope that will help you dude. – Shahzad Khan Feb 23 '15 at 17:01
  • Unfortunately it did not solve the problem but thank you anyways ;) – oHoodie Feb 25 '15 at 06:20
  • 2
    Since your iterating through `Model.SelectedSites` I assume its supposed to be `@Html.DropDownListFor(m => m.SelectedSites[i], ...` (not `SelectedCountries`). Unfortunately `DropDownListFor()` does not work as expected in a `for` loop. You can create a `EditorTemplate` or generate a new `SelectList` in each iteration and use the 4th parameter to set the value. –  Feb 26 '15 at 01:00
  • Thanks a lot! Once again you saved my day. `SelectedCountries` was right but thanks for the hint with the 4th parameter. – oHoodie Feb 26 '15 at 08:35
  • @oHoodie, If `SelectedCountries` was indeed correct, then I suspect you have some issues with your view model (it appears your creating multiple arrays of value types instead of creating a collection of objects containing properties such as `SelectedSite` and `SelectedCountry`) –  Feb 27 '15 at 02:28
  • @StephenMuecke I ran into issues when I used objects so I just split it in two lists of simple data types and it worked. Probably not the most elegant solution but it works for now... – oHoodie Feb 27 '15 at 06:53
  • Its a bad design and you should be using objects. And if you do then you need a custom `EditorTemplate` for the object and pass the `SelectList` to the `EditorTemplate` as additional ViewData (refer option 2 of [this answer](http://stackoverflow.com/questions/26162218/editortemplate-for-dropdownlist/26417466#26417466) –  Feb 27 '15 at 06:57

0 Answers0