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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:09:11+00:00 2026-06-01T14:09:11+00:00

I tried searching and didn’t find anything that fixed my problem. I have a

  • 0

I tried searching and didn’t find anything that fixed my problem. I have a DropDownList on a Razor view that will not show the the item that I have marked as Selected in the SelectList. Here is the controller code that populates the list:

var statuses  = new SelectList(db.OrderStatuses, "ID", "Name", order.Status.ID.ToString());
ViewBag.Statuses = statuses;
return View(vm);

Here is the View code:

<div class="display-label">
   Order Status</div>
<div class="editor-field">
   @Html.DropDownListFor(model => model.StatusID, (SelectList)ViewBag.Statuses)
   @Html.ValidationMessageFor(model => model.StatusID)
</div>

I walk through it and even in the view it has the correct SelectedValue however the DDL always shows the first item in the list regardless of the selected value. Can anyone point out what I am doing wrong to get the DDL to default to the SelectValue?

  • 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-06-01T14:09:12+00:00Added an answer on June 1, 2026 at 2:09 pm

    The last argument of the SelectList constructor (in which you hope to be able to pass the selected value id) is ignored because the DropDownListFor helper uses the lambda expression you passed as first argument and uses the value of the specific property.

    So here’s the ugly way to do that:

    Model:

    public class MyModel
    {
        public int StatusID { get; set; }
    }
    

    Controller:

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            // TODO: obviously this comes from your DB,
            // but I hate showing code on SO that people are
            // not able to compile and play with because it has 
            // gazzilion of external dependencies
            var statuses = new SelectList(
                new[] 
                {
                    new { ID = 1, Name = "status 1" },
                    new { ID = 2, Name = "status 2" },
                    new { ID = 3, Name = "status 3" },
                    new { ID = 4, Name = "status 4" },
                }, 
                "ID", 
                "Name"
            );
            ViewBag.Statuses = statuses;
    
            var model = new MyModel();
            model.StatusID = 3; // preselect the element with ID=3 in the list
            return View(model);
        }
    }
    

    View:

    @model MyModel
    ...    
    @Html.DropDownListFor(model => model.StatusID, (SelectList)ViewBag.Statuses)
    

    and here’s the correct way, using real view model:

    Model

    public class MyModel
    {
        public int StatusID { get; set; }
        public IEnumerable<SelectListItem> Statuses { get; set; }
    }
    

    Controller:

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            // TODO: obviously this comes from your DB,
            // but I hate showing code on SO that people are
            // not able to compile and play with because it has 
            // gazzilion of external dependencies
            var statuses = new SelectList(
                new[] 
                {
                    new { ID = 1, Name = "status 1" },
                    new { ID = 2, Name = "status 2" },
                    new { ID = 3, Name = "status 3" },
                    new { ID = 4, Name = "status 4" },
                }, 
                "ID", 
                "Name"
            );
            var model = new MyModel();
            model.Statuses = statuses;
            model.StatusID = 3; // preselect the element with ID=3 in the list
            return View(model);
        }
    }
    

    View:

    @model MyModel
    ...    
    @Html.DropDownListFor(model => model.StatusID, Model.Statuses)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I tried searching a bit and didn't find an answer. Does the Razor View
I tried googling and searching on stack but I didn't find anything :-( (
I tried to find a solution but I didn't found what I was searching
Tried searching the site, but cannot find an answer to my problem: Lets say
I tried searching for this but couldn't find a similar question. I have a
I tried searching in google but could not find any useful answer, if(verifier.length() >
I have tried searching for it online, but I got confused. I didn't get
I tried searching this on Google and didn't really learn anything as the search
Sorry, but I tried searching for this subject, but I didn't find related to
I don't know anything about java nor html. I tried searching but didn't get

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.