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

  • Home
  • SEARCH
  • 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 8878987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:50:47+00:00 2026-06-14T19:50:47+00:00

I am using VS 2010 with MVC 3 and EF 5. I am using

  • 0

I am using VS 2010 with MVC 3 and EF 5. I am using a common pattern for dropdown lists that works correctly in all but one instance and I cannot see why this one is failing to select the correct entry in the select list. The following are code snippets.

The select list is created as follows:

   public static IEnumerable<SelectListItem> GetOutcomes()
    {
        CodesEntities dataContextCodes = new CodesEntities(ConnectionString);

        return new SelectList(dataContextCodes.CodeOutcome.
            Where(x => x.DisplayOrder > 0).OrderBy(x => x.DisplayOrder), 
            "OutcomeCodeID", "Outcome");
    }

This returns the correct select list.

The view has the following code:

@Html.DropDownListFor(m => m.OutcomeCodeID,
                PerintalFormViewModels.GetOutcomes(), "Please select an item")

The model value m.OutcomeCodeID has a valid value (1) but no item is being selected.

The generated HTML is:

<select id="CodeID" name="OutcomeCodeID" data-val-required="Outcome is required" data-val-number="The field outcome must be a number." data-val="true">
<option value="">Please select an item</option>
<option value="1">Termination</option>
<option value="2">Loss</option>
<option value="3">Still</option>
<option value="4">Live</option>
</select>

I am in the hair tearing out, being driven nuts stage. Does anyone have any thoughts?

Thanks

  • 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-14T19:50:48+00:00Added an answer on June 14, 2026 at 7:50 pm

    You can do it like this:

    public static IEnumerable<SelectListItem> GetOutcomes(string selectedID)
    {
        CodesEntities dataContextCodes = new CodesEntities(ConnectionString);
    
    
        return new SelectList(dataContextCodes.CodeOutcome.
            Where(x => x.DisplayOrder > 0).OrderBy(x => x.DisplayOrder), 
            "OutcomeCodeID", "Outcome"
            , selectedID); // add this parameter
    }
    

    Or this:

    public static IEnumerable<SelectListItem> GetOutcomes(string selectedID)
    {
        CodesEntities dataContextCodes = new CodesEntities(ConnectionString);
    
    
        return        
          dataContextCodes.CodeOutCome
          .Where(x => x.DisplayOrder > 0)
          .OrderBy(x => x.DisplayOrder)
          .ToList()
          .Select(x => new SelectListItem 
          { 
              Value = x.OutcomeCodeID.ToString(),
              Text = x.Outcome.ToString(),
              Selected = x.OutcomeCodeID == selectedID
          });
    }
    

    Then call it like this:

    @Html.DropDownListFor(m => m.OutcomeCodeID,
                PerintalFormViewModels.GetOutcomes(Model.OutcomeCodeID), 
                "Please select an item")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using VS 2010 with ASP.net MVC 2. I recieved a program that was
I am using SharePoint 2010 and have a customized newform.aspx and editform.aspx that I
I create a ASP.net MVC 4 project by using Visual Studio 2010 IDE. I
I am currently developing an ASP.NET MVC 3 site using Visual Studio 2010 with
I am using Visual Studio 2010, but this applies to VS 2008 as well.
Using VS 2010 beta 2, ASP.NET MVC. I tried to create an Entity framework
I have been working on an MVC 3 app. I was using VS 2010's
I'm going to be building some ASP.Net MVC 2 software using Visual Studio 2010
Using Visual Studio 2010, MVC project When my form is submitted (currently via javascript,
Setup: I am using MVC 3, EF 4.1, Visual Studio 2010 SP1 with Power

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.