I am dynamically loading a partial class with dropdownlists. The dropdownlists represent the many side of a one to many relationship. One person registering with multiple apartments. See pic below. Its all working up to this point. However, when I post back the form, I want the default model binder to collect all the selected values into the List in the viewmodel.
public class RegistrationViewModel
{
public RegisterModel RegistrationData { get; set; }
public List<OccupierApartment> OccupierApartment { get; set; }
public SelectList ComplexesSelectList { get; set; }
public SelectList OccupierTypeSelectList { get; set; }
}
This is my question: What is the lambda syntax for the dropdownlists in the view: Something like this, but its only getting the first item in the list, whereas I need to get all the selected dropdownlists : @Html.DropDownListFor(m => m.OccupierApartment[0].OccupierTypeID, Model.OccupierTypeSelectList, “– select –“)
I would recommend you taking a look at the following article which illustrates a custom HTML helper which is adapted to this scenario of dynamically adding elements to a list.