MVC DropDownListFor - Selected Value ViewModel Class

Joseph Anderson

I am trying populate the selected value of a dropdown list. When I set the selected value on my ViewModel as a string, it works perfectly. When I set the selected value property as part of a subclass, it fails:

@Html.DropDownListFor(model => model.JMASettings.CustomerID, Model.CustomerIDList)

This method works:

@Html.DropDownListFor(model => model.CustomerID, Model.CustomerIDList)
  1. Is it OK to have classes inside of the ViewModel?

    public class SettingsViewModel
    {
        public SettingsViewModel()
        {
            JMASettings = new JMASettings();
        }
    
        public JMASettings JMASettings {get; set;}
    }
    
    public class JMASettings
    {
        public string CustomerID {get; set;}
    }
    
  2. Do you recommend that I copy and paste the contents of JMASettings into my settings view model class?

Chris Pratt
  1. Sure. Your view model can contain whatever you like.

  2. Not necessarily. There's no functional reason your first attempt using model.JMASettings.CustomerID shouldn't show the selected value. If it's not, then there's some other problem. You may not be properly populating the CustomerID property on JMASettings instance, or something in ModelState may be overriding what you set. ModelState is composed of values from Request, ViewData and ViewBag, first, and only values from Model as a last resort.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

MVC DropDownListFor selected value not being selected

From Dev

Selected value for dropdownlistfor in mvc4

From Dev

dropdownlistfor selected value with viewdata

From Dev

DropDownListFor selected value not working

From Dev

MVC, DropDownListFor in partialview, onchange should store selected value _somewhere_

From Dev

Kendo MVC DropDownListFor does not bind selected value to model

From Dev

ASP.NET MVC @Html.DropdownListFor wrong selected value

From Dev

MVC DropdownlistFor set selected value not working?c#

From Dev

MVC - Set selected value of SelectList inside ViewModel

From Dev

DropDownListFor not always showing selected value

From Dev

'dropdownlistfor' controller not taking the selected value

From Dev

Dropdownlistfor MVC 5 in ListView (IEnumerable of Viewmodel)

From Dev

Dropdownlistfor MVC 5 in ListView (IEnumerable of Viewmodel)

From Dev

MVC DropDownListFor Bind on Nested Class

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

Getting selected Index from DropDownListFor MVC

From Dev

MVC update ListboxFor with selected values from DropDownListFor

From Dev

Getting selected Index from DropDownListFor MVC

From Dev

MVC update ListboxFor with selected values from DropDownListFor

From Java

Html.DropdownListFor selected value not being set

From Dev

Using selected value in @Html.DropDownListFor

From Dev

DropDownListFor with an item value 0 always selected

From Dev

Unable to get @Html.DropDownListFor Selected Value

From Dev

Html.DropDownListFor gives error if no value selected

From Dev

DropDownListFor not setting selected value with different names

From Dev

Html.DropDownListFor set selected value

From Dev

MVC DropDownListFor with ViewBag not passing value to controller

From Dev

MVC - DropDownListFor Enum - Set Default Value

Related Related

  1. 1

    MVC DropDownListFor selected value not being selected

  2. 2

    Selected value for dropdownlistfor in mvc4

  3. 3

    dropdownlistfor selected value with viewdata

  4. 4

    DropDownListFor selected value not working

  5. 5

    MVC, DropDownListFor in partialview, onchange should store selected value _somewhere_

  6. 6

    Kendo MVC DropDownListFor does not bind selected value to model

  7. 7

    ASP.NET MVC @Html.DropdownListFor wrong selected value

  8. 8

    MVC DropdownlistFor set selected value not working?c#

  9. 9

    MVC - Set selected value of SelectList inside ViewModel

  10. 10

    DropDownListFor not always showing selected value

  11. 11

    'dropdownlistfor' controller not taking the selected value

  12. 12

    Dropdownlistfor MVC 5 in ListView (IEnumerable of Viewmodel)

  13. 13

    Dropdownlistfor MVC 5 in ListView (IEnumerable of Viewmodel)

  14. 14

    MVC DropDownListFor Bind on Nested Class

  15. 15

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

  16. 16

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

  17. 17

    Getting selected Index from DropDownListFor MVC

  18. 18

    MVC update ListboxFor with selected values from DropDownListFor

  19. 19

    Getting selected Index from DropDownListFor MVC

  20. 20

    MVC update ListboxFor with selected values from DropDownListFor

  21. 21

    Html.DropdownListFor selected value not being set

  22. 22

    Using selected value in @Html.DropDownListFor

  23. 23

    DropDownListFor with an item value 0 always selected

  24. 24

    Unable to get @Html.DropDownListFor Selected Value

  25. 25

    Html.DropDownListFor gives error if no value selected

  26. 26

    DropDownListFor not setting selected value with different names

  27. 27

    Html.DropDownListFor set selected value

  28. 28

    MVC DropDownListFor with ViewBag not passing value to controller

  29. 29

    MVC - DropDownListFor Enum - Set Default Value

HotTag

Archive