Html.DropDownList selected value not working using ViewBag

后端 未结 5 1101
谎友^
谎友^ 2021-01-05 20:10

Well, after a couple hours reading stuff over here, trying unsuccessfully all the solutions, also found this article that i thought it would save my life... nothing.

<
5条回答
  •  星月不相逢
    2021-01-05 20:59

    ViewBag.yearDropDown = new SelectList(years, "Value", "Text", years.Where(x => x.Selected).FirstOrDefault());
    

    Last parameter here is SelectListItem, but it must be selected value (string in your example)

    SelectList Constructor (IEnumerable, String, String, Object)

提交回复
热议问题