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)
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;}
}
Do you recommend that I copy and paste the contents of JMASettings into my settings view model class?
Sure. Your view model can contain whatever you like.
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.
Comments