DropDownListFor does not select value if in for loop

后端 未结 6 1153
你的背包
你的背包 2020-12-05 17:49

In my view

<%= Html.DropDownListFor( x => x.Countries[ i ], Model.CountryList )%>

in my controller

public int[ ] C         


        
6条回答
  •  伪装坚强ぢ
    2020-12-05 18:42

    A select box sends a single value, so the Countries property should not be an array. Also in your post it is not clear where's the i variable you are using in your lambda expression coming from and this x.CountryList used in the helper won't compile as x is not defined.

    Model:

    public class MyModel
    {
        public int SelectedCountry { get; set; }
        public List CountryList { get; set; }
    }
    

    View:

    <%= Html.DropDownListFor(x => x.SelectedCountry, Model.CountryList) %>
    

    UPDATE:

    According to the comment it seems that there are multiple drop downs. I suspect that the problem might come from the i variable used as index in a for loop.

    You might try this instead:

    Model:

    public class MyModel
    {
        public int[] Countries { get; set; }
        public List CountryList { get; set; }
    }
    

    View:

    <% foreach (var country in Model.Countries) { %>
        <%= Html.DropDownListFor(x => country, Model.CountryList) %>
    <% } %>
    

提交回复
热议问题