Create DropDownListFor from SelectList with default value

前端 未结 5 1207
花落未央
花落未央 2021-01-05 08:33

I have a dropdownlistfor:

 @Html.DropDownListFor(model => model.Item.Item.Status, new SelectList(@Model.AllStatus, \"id\", \"Description\"),          


        
5条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-05 08:38

    There exist already some discussions about that here or there. One of the problems might be using a different type than string for the key value. I had similar problems in past and I know that i solved it like this - explicitly setting the Selected property when preparing the list (in your case, AlLStatus).

    Would mean, for your case (in controller action):

    IEnumerable selectList = 
    from s in allStatus // where ever you get this from, database etc.
    select new SelectListItem
    {
        Selected = (s.id == model.Item.Item.Status),
        Text = cs.Description,
        Value = s.id.ToString()
    };
    model.AllStatus = selectList;
    

提交回复
热议问题