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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T05:00:42+00:00 2026-05-21T05:00:42+00:00

Using ASP.NET MVC 3, I have been struggling to set the selected value for

  • 0

Using ASP.NET MVC 3, I have been struggling to set the selected value for dropdown using the help Html.DropDownListFor() function for a strongly typed view.

The code for the models look like this:

public class ResourceView
{
   ...
   public Language Language { get; set; }
}

public class Language
{
   public string DisplayName { get; private set; }      
   public string Code { get; private set; }    
   public override string ToString() {  return DisplayName;  }
}

and within the view:

@Html.DropDownListFor(model => model.Language, 
  new SelectList(ViewBag.AllPossibleLanguages, 
    "Code", "DisplayName", Model.Language.Code))

There are a few workarounds, such as using

@Html.DropDownList("LanguageCode", 
   new SelectList(ViewBag.PossibleLanguages,
      "Code", "DisplayName", Model.Language.Code))

and curiously enough, the code above won’t work if the first parameter is set as “Language”, same as the name of the property in the model.

I can also do like this

@Html.DropDownListFor(model => model.Language, 
  new SelectList(ViewBag.AllPossibleLanguages))

which will work as well with the appropriate selected value, however only the ToString() value of the Language class will be displayed and the actual language code that i want in the option tags won’t be set, which is not surprising as there is nothing identify that the Code field should be used as the value attribute.

I can also solve this using a jquery event handler that selects the appropriate value afterwards. However I am curious as to why this doesn’t work as intended and would like to solve it properly if possible.

  • 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-21T05:00:43+00:00Added an answer on May 21, 2026 at 5:00 am

    The first argument for @Html.DropDownListFor is the property of your model the selected value will be bound to on Post. When you say @Html.DropDownListFor(model => model.Language) the default binder won’t able to figure which property in your Language class it needs to bind the selected value which in the case of dropdown list will always be a single string or integer. For the same reason @Html.DropDownList("LanguageCode") works.

    You can always extended the default model binder if you would like to change the behavior.

    Edit:

    You could try, which I believe should solve your problem in this case.

    @Html.DropDownListFor(model.Language.Code, new SelectList(ViewBag.AllPossibleLanguages))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been using Telerik Charts with Asp.net mvc and I have run into
I'm using ASP.NET MVC RC2. I have a set of classes that were auto-generated
I've been developing an application using asp.net MVC, and I have some configurations that
I have been using telerik extensions for an asp.net mvc project, more specifically the
i have been building websites using vs 2008 (mostly asp.net mvc, jquery and webforms).
I have been using ASP.NET MVC 3 for a while to implement a testing
I have been using ASP.NET MVC for six months or so and have been
I have been using ASP.NET MVC for the past few months, after successfully ignoring
Newbie question. I’m writing an ASP.Net MVC app in VB.Net and have been using
I have been learning to develop websites using ASP.NET MVC 2 for work... and

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.