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 6220269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:55:44+00:00 2026-05-24T07:55:44+00:00

The dropdown list will not select the value I need Dictionary<int,int> RowDict = new

  • 0

The dropdown list will not select the value I need

 Dictionary<int,int> RowDict = new Dictionary<int, int>();
 for (int i = 1; i < 11; i++)
 {
      RowDict.Add(i, i);
 }
 var rows = new SelectList(RowDict, "Key", "Value", 6);
 ViewBag.Rows = rows;

here is the code in the view:

  <div class="editor-label">
       @Html.LabelFor(m => m.Rows)
  </div>

 <div class="editor-field">
      @Html.DropDownListFor(m => m.Rows, (SelectList)ViewBag.Rows)
      @Html.ValidationMessageFor(m => m.Rows)
 </div>

When I run the code 6 is not selected. Can you see any errors in this code? The first value is 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-24T07:55:44+00:00Added an answer on May 24, 2026 at 7:55 am

    m => m.Rows suggests there is a model involved. It works better if you do the initialization with the rows from the model, not the view bag.

    A really quick and dirty initialization like this :

       public class TestModel
       {
          public TestModel()
          {
             Dictionary<int, int> RowDict = new Dictionary<int, int>(); 
             for (int i = 1; i < 11; i++) 
             { 
                RowDict.Add(i, i); 
             }
    
             this.Rows = new SelectList(RowDict, "Key", "Value");
             this.SelectedId = "6"; // binding to selection works as a String
          }
    
          public SelectList Rows { get; set; }
    
          public string SelectedId { get; set; }
    
       }
    

    And this in the controller where the view is requested :

    return View(new TestModel());
    

    And a view starting with something like :

    @model <insert_appropriate_anmespace_here>.TestModel
    

    …

    @Html.DropDownListFor(m => m.Rows, Model.Rows, Model.SelectedId) 
    

    makes it work. You can adapt this code and initialization the way you want, as long as you remember that models are passed by value to the view, reason why I setup my init in the constructor (every time a TestModel is created, the Select List will get initialized).

    Also you can make a mix of viewbag and model and have it to work, I wouldnt recommend it for readability.

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

Sidebar

Related Questions

I need a dropdown list on my page that will allow a user to
So I have a dropdown list <select id=theSelectId> <option value=volvo>Volvo</option> <option value=saab>Saab</option> <option value=mercedes>Mercedes</option>
I'm trying to build an AutoComplete DropDown List that will work on a Mobile
I'd like to add a drop-down list to a Windows application. It will have
I have a dropdown list that stores name/value pairs. The dropdown appears in each
I have dropdown list control in one of my application and when I add
I'm new to Outlook add-in programming and not sure if this is possible: I
I'm working on a double drop down menu a'la http://http://www.coursesweb.net/ajax/multiple-select-dropdown-list-ajax_t See http://www.hafdal.dk/testing/test.php I'm working
I'm trying to create a view that will need 2 dropdown lists with MVC
I've got a dropdown list that is being populated via a webservice using ASP>NET

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.