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

前端 未结 2 835
夕颜
夕颜 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条回答
  • 2021-01-04 06:24

    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).

    0 讨论(0)
  • 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<SelectListItem>)
    

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

    [HttpPost]
    public ActionResult Create(Licenses model)
    {
      //check model.SelectedLicenseName
    }  
    
    0 讨论(0)
提交回复
热议问题