@Html.DropDownListFor requires a List of SelectListItem. Is there a
way to use a custom object here instead of select list item and tell
the drop down list which properties of the custom object to use for
the value and text?
You could use the SelectList constructor
taking 3 arguments: an IEnumerable<T>
and 2 strings representing the names of the value and text properties of the custom type:
@Html.DropDownListFor(
x => x.SelectedStatusCode,
new SelectList(
Model.Satuses,
"StatusCode",
"StatusCodeDescription"
)
)
In this example we assume that Model.Satuses
is a property of type IEnumerable<StatusViewModel>
where StatusViewModel
contains at least 2 properties to bind the respectively the value and the text of the dropdown:
public class StatusViewModel
{
public string StatusCode { get; set; }
public string StatusCodeDescription { get; set; }
...
}