Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4007530
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:38:36+00:00 2026-05-20T08:38:36+00:00

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

  • 0

In my view

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

in my controller

public int[ ] Countries { get; set; }

public List<SelectListItem> CountryList { get; set; }

When the forms gets posted there is no problem, the dropdown is populated and the values the user selects are posted. But when I try to load the form with already assigned values to the Countries[ ] it does not get selected.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T08:38:37+00:00Added an answer on May 20, 2026 at 8:38 am

    I’m getting the same too. When using foreach to loop around a DropDownListFor (i.e. to render multiple select elements on a page).

    My work around is to set the selected value in the controller rather than the view: something like this:

    In the controller:

    public class FruitList 
    {        
        public int? selectedFruit{ get; set; }
        public List<SelectListItem> fruits
        {
            get
            {
                fruitEntities F = new fruitEntities();
                List<SelectListItem> list = (from o in F.Options
                                             select new SelectListItem
                                             {
                                                 Value = o.fruitID,
                                                 Text = o.fruit,                                                 
                                                 Selected = o.fruitID == selectedFruit
                                             }).ToList();
                return list;
            }
        }
    }
    
    public class ViewModel 
    {              
        public List<FruitList> collectionOfFruitLists { get; set; }        
    }
    

    In the view

    <table>                        
       <% for (int i=0; i < Model.collectionOfFruitLists.Count; i++ )
            { %>
            <tr>                            
                <td><%: Html.DropDownList("fruitSelectList", collectionOfFruitLists[i].fruits, "Please select...") %></td>                
            </tr>
            <%} %>
     </table>
    

    The nifty bit is Selected = o.fruitID == selectedFruit in the controller which acts like a SQL CASE statement; this is really well explained by Lance Fisher (thanks Lance, your post really helped me out 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Model: public class Model { public ItemType Type { get; set; } public int
In my viewModel I have public string Addressline1 { get; set; } public List<SelectListItem>
I have created a partial view that displays a dropdownlist html.DropDownListFor(m => m.SelectOption, Model.SelectOption)
I have this my view @Html.DropDownListFor(model => model.StateCode, Model.StateList, Select, new { @style =
I'm using DropDownListFor in my view: <%: Html.DropDownListFor(model => model.Type, new SelectList(ComponentsFactory.GetTypeList().Select(a => new
I have the following in my View: @Html.DropDownListFor(model => model.Search, new SelectList(Model.SearchOptions)) In my
Hi I have a line in my view like: @Html.DropDownListFor(modelItem => item.CheckerId, Model.StaffByArea as
When I have a DropDownList that relevant to Model of view like this: @Html.DropDownListFor(model
I have a MVC view that contains some dropdowns like this : <%: Html.DropDownListFor(model
I've got the following ASP.NET 4 MVC 3 code in a view: @Html.TextBoxFor(model =>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.