Set selected value for DropDownListFor in controller does not work in razor view

JP Hellemons

I have this in my controller:

[Route("Checkout")]
public ActionResult Checkout()
{
    var sb = new ShoppingBagViewModel();
    sb.DestinationCountry = "Netherlands"; // I hoped that this was sufficient
    sb.CountriesSl.Single(c => c.Text.Equals("Netherlands", StringComparison.InvariantCultureIgnoreCase)).Selected = true;
    return View(sb);
}

Quick watch in Visual Studio confirmed that the CountriesSl (which is of type List<SelectListItem>) has a selected value. (netherlands)

This is my razor view:

@Html.DropDownListFor(m => m.DestinationCountry, Model.CountriesSl)

DestinationCountry is also a string prop in my viewmodel. I know that there are a lot of similar questions, but I have looked at a lot of them and I just do not see it. I also tried:

@Html.DropDownListFor(m => Model.DestinationCountry, Model.CountriesSl)

Please do not just give me the answer, but also explain what my mistake is.

edit to make clear what the problem is: I get a nice option list in my razor view, but there is no item "selected" just the first one. When I look at the generated html, there is no selected item too.

edit for Ric

public List<SelectListItem> CountriesSl
{
    get
    {
        List<SelectListItem> _countries = HttpContext.Current.Cache["slCountries"] as List<SelectListItem>;
        if (_countries == null)
        {
            _countries = new List<SelectListItem>();
            foreach (string s in System.IO.File.ReadLines(System.Web.Hosting.HostingEnvironment.MapPath("~/tmpwrite/countries.csv")))
            {
                var tmp = s.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                _countries.Add(new SelectListItem() { Text = tmp[1], Value = tmp[0], Selected = false });
            }
            HttpContext.Current.Cache.Add("slCountries", _countries.OrderBy(c => c.Text).ToList(), null, System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(24, 0, 0), System.Web.Caching.CacheItemPriority.Normal, null);
        }
        return _countries;
    }
}
Mattias Åslund

DropDownListFor tries to automatically match the value of the receiving property in the provided enumeration. I also had problems with this in the beginning.

Instead provide any list of entities in the viewmodel. I usually use something like IDictionary<int, string>. Then create the dropdown like this:

Html.DropDownListFor(m=>m.DestinationCountry, new SelectList(Model.Countries, "Key", "Value"))

Then the SelectList will automagically set the correct option where its Key matches DestinationCoutry.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

'dropdownlistfor' controller not taking the selected value

From Dev

MVC5 Razor html.dropdownlistfor set selected when value is in array

From Dev

MVC5 Razor html.dropdownlistfor set selected when value is in array

From Dev

How to get value selected in @Html.DropDownListFor inside the view and pass it to action in OTHER controller?

From Java

Html.DropdownListFor selected value not being set

From Dev

Html.DropDownListFor set selected value

From Dev

How to set Default value in MVC 4 razor DropDownListFor

From Dev

dropdownlistfor selected value with viewdata

From Dev

DropDownListFor selected value not working

From Dev

How to set default selected value for Html.DropDownListFor

From Dev

MVC DropdownlistFor set selected value not working?c#

From Dev

DropDownList selected value on a razor view page

From Dev

Kendo MVC DropDownListFor does not bind selected value to model

From Dev

Using DropDownListFor jquery does not get the currently selected value

From Dev

Set Dropdownlist Selected Value in the View

From Dev

DropDownListFor not always showing selected value

From Dev

Passing selected button value from view to controller

From Dev

Razor - Passing DropDownList selected value to Controller via Ajax.BeginForm

From Dev

MVC DropDownListFor selected value not being selected

From Dev

iOS addSubview of a new view controller does not work

From Dev

Methods working in view does not work in controller

From Dev

Binding does not work between view and controller

From Dev

ng-selected does not work with ng-repeat to set default value

From Dev

MVC DropDownListFor - Selected Value ViewModel Class

From Dev

Using selected value in @Html.DropDownListFor

From Dev

DropDownListFor with an item value 0 always selected

From Dev

Selected value for dropdownlistfor in mvc4

From Dev

Unable to get @Html.DropDownListFor Selected Value

From Dev

Html.DropDownListFor gives error if no value selected

Related Related

  1. 1

    'dropdownlistfor' controller not taking the selected value

  2. 2

    MVC5 Razor html.dropdownlistfor set selected when value is in array

  3. 3

    MVC5 Razor html.dropdownlistfor set selected when value is in array

  4. 4

    How to get value selected in @Html.DropDownListFor inside the view and pass it to action in OTHER controller?

  5. 5

    Html.DropdownListFor selected value not being set

  6. 6

    Html.DropDownListFor set selected value

  7. 7

    How to set Default value in MVC 4 razor DropDownListFor

  8. 8

    dropdownlistfor selected value with viewdata

  9. 9

    DropDownListFor selected value not working

  10. 10

    How to set default selected value for Html.DropDownListFor

  11. 11

    MVC DropdownlistFor set selected value not working?c#

  12. 12

    DropDownList selected value on a razor view page

  13. 13

    Kendo MVC DropDownListFor does not bind selected value to model

  14. 14

    Using DropDownListFor jquery does not get the currently selected value

  15. 15

    Set Dropdownlist Selected Value in the View

  16. 16

    DropDownListFor not always showing selected value

  17. 17

    Passing selected button value from view to controller

  18. 18

    Razor - Passing DropDownList selected value to Controller via Ajax.BeginForm

  19. 19

    MVC DropDownListFor selected value not being selected

  20. 20

    iOS addSubview of a new view controller does not work

  21. 21

    Methods working in view does not work in controller

  22. 22

    Binding does not work between view and controller

  23. 23

    ng-selected does not work with ng-repeat to set default value

  24. 24

    MVC DropDownListFor - Selected Value ViewModel Class

  25. 25

    Using selected value in @Html.DropDownListFor

  26. 26

    DropDownListFor with an item value 0 always selected

  27. 27

    Selected value for dropdownlistfor in mvc4

  28. 28

    Unable to get @Html.DropDownListFor Selected Value

  29. 29

    Html.DropDownListFor gives error if no value selected

HotTag

Archive