Why I am getting “System.Web.Mvc.SelectListItem” in my DropDownList?

前端 未结 2 847
夕颜
夕颜 2021-01-04 05:58

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          


        
2条回答
  •  梦毁少年i
    2021-01-04 06:28

    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)
    

    So when you post your form, you can inspect the SelectedLicenseName property

    [HttpPost]
    public ActionResult Create(Licenses model)
    {
      //check model.SelectedLicenseName
    }  
    

提交回复
热议问题