I believe I have bound my data correctly, but I can\'t seem to get my text property for each SelectListItem to show properly.
My model:
public class
I explicitly set the dataValueField
and dataTextField
names.
new SelectListItem
{
Value = row["Description"].ToString(),
Text = "test"
}), "Value", "Text");
Then there's no need to write Model.LicenseNames.Items as List<SelectListItem>
in your views (as suggested in your accepted answer).
Use the Items
property of your LicenseNames
property which is of type SelectList
@Html.DropDownList("SelectedLicenseName", new SelectList(Model.LicenseNames.Items,
"Value", "Text", Model.LicenseNames.SelectedValue))
Or with the DropDownListFor
helper method
@Html.DropDownListFor(d=>d.SelectedLicenseName,
Model.LicenseNames.Items as List<SelectListItem>)
So when you post your form, you can inspect the SelectedLicenseName
property
[HttpPost]
public ActionResult Create(Licenses model)
{
//check model.SelectedLicenseName
}