public class State
{
public Guid Id { get; set; }
public string Name { get; set; }
}
public class Address
{
public State State { get; set; }
}
public class JobSeeker
{
public Address CurrentAddress { get; set; }
}
public class RegisterVM
{
public JobSeeker JobSeeker { get; set; }
public List<State> AllStates { get; set; }
}
in Razor
@Html.DropDownListFor(m => m.JobSeeker.CurrentAddress.State,
new SelectList(Model.AllStates, "Id", "Name" ), " -----Select List----- ")
The result is the drop down is populated with the value present in AllStates, but the problem is m.JobSeeker.CurrentAddress.State is null when posted to controller action. How to set the selected value of dropdown to property m.JobSeeker.CurrentAddress.State
Stateis a complex object and a<select>posts back a single value, not a complex object. It would need to be@Html.DropDownListFor(m => m.JobSeeker.CurrentAddress.State.ID, ....)But do not do that!. Add apublic Guid SelectedStata { get; set; }property toRegisterVMand bind to that.