DropDownList not behaving as expected

I'm having trouble with my DropDownListFors that I'm hoping you can help me with. I'm guessing it's one of those things that you either know or you don't.

The problem is I have a Countries table in my database which has a list of countries in it. The behaviour I would like from my drop down is to create a foreign key reference in my Address table pointing to the county selected in the drop down. The behaviour I'm getting is that the foreign key in my Address table is pointing to a new entry in the Country which is totally unwanted.

Can anyone explain how to do this? I'm not sure what code you guys would like to see so please let me know if you can help.

=== More information ===

Ok, I have a view model class like this:

public class CountryViewModel { public int CountryId { get; set; } public string Name { get; set; } }

and in my view I have a drop down list like this:

@Html.DropDownListFor(m => m.LegalEntity.Address.Country.CountryId, new SelectList( Model.LegalEntity.Address.Country, "CountryId", "Name", Model.LegalEntity.Address.Country.CountryId), new { @class = "form-control" })

Note that the second line of this does not currently work: I don't know how to get the entire list of countries into this.

My legal entity view model looks like this:

public class LegalEntityViewModel { [Key] public int LegalEntityID { get; set; } public virtual AddressViewModel Address { get; set; } public virtual TechnicalContactViewModel TechnicalContact { get; set; } }

and my address view model looks like this:

public class AddressViewModel { [Key] public int AddressID { get; set; } ... [Display(Name = "Country")] public virtual CountryViewModel Country { get; set; } }

The behaviour I would like is for all the countries to populate the drop down and the selected country to end in my LegalEntityViewModel.AddressViewModel.CountryViewModel.

Help! I've been fiddling with this and refactoring all day!

Looking forward to your responses.



There's multiple ways to do this. For example you could load the list of countries in you AddressViewModel.


public class AddressViewModel { [Display(Name = "Country")] public int SelectedCountryId { get; set; } public IEnumerable<SelectListItem> Countries { get; set; } }

Then in your view do this

@Html.DropDownListFor(m => m.SelectedCountryId , new SelectList(Model.Countries , "Value", "Text"))

You could also load you Countries list with Javascript.

$(document).ready(function () { $.ajax({ type: 'POST', url: '@Url.Action("GetCountries")', <--This will be a method in your controller that brings back the Countries, success: function (results) { var options = $('#SelectedCountryId'); $.each(results, function () { options.append($('<option />').val(this.CountryId).text(this.CountryName)); }); } }); public class CountryViewModel { public int CountryId {get;set;} public int CountryName {get;set; }

In your controller

[HttpPost] public JsonResult GetCountries() { var countries = //some method to get the countries for a database or something var countriesList = countries .Select(x => new CountryViewModel { CountryId = x.CountryId, CountryName = x.CountryName }).ToList(); return this.Json(countriesList ); }


  • Setting default selected value of selectlist inside an editor template
  • How To display user Friendly name from Enum in DropDownList in MVC View?
  • ASP.NET MVC3 Validation of nested view model object fields
  • Why is this Exception?- The relationship between the two objects cannot be defined because they are
  • Working with EditorTemplates and radio buttons
  • Setting default value to Html.DropDownListFor
  • Dynamic DropDownLists In MVC 4 Form
  • Passing Props from State to Child
  • Access user's phone number on iOS 7
  • Servlet stops working on Tomcat server after some hits or time
  • Scipy Leastsq Optional Output Variable (Mesg)
  • Can someone please explain to me in the most layman terms how to use EventArgs?
  • Eliminate partial duplicate rows from result set
  • Get history of file changes from TFS to implement custom “blame”-behaviour of exceptions
  • Keep this build forever option - Jenkins
  • iOS: Detect app start via notification press
  • Lost migrations and Azure database is now out of sync
  • How would I use PHP exceptions to define a redirect?
  • DirectX11 ClearRenderTargetViewback with transparent buffer?
  • How to set my toolbar fixed while scrolling android
  • Why is the timeout on a windows udp receive socket always 500ms longer than set by SO_RCVTIMEO?
  • Web-crawler for facebook in python
  • AT Commands to Send SMS not working in Windows 8.1
  • How to get icons for entities from eclipse?
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Unit Testing MVC Web Application in Visual Studio and Problem with QTAgent
  • Proper way to use connect-multiparty with express.js?
  • Load html files in TinyMce
  • How to set the response of a form post action to a iframe source?
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Getting Messege Twice Using IMvxMessenger
  • Change div Background jquery
  • Qt: Run a script BEFORE make
  • apache spark aggregate function using min value
  • JaxB to read class hierarchy
  • Sorting a 2D array using the second column C++
  • reshape alternating columns in less time and using less memory
  • java string with new operator and a literal
  • How to load view controller without button in storyboard?