Html.DropDownList selected value not working using ViewBag

后端 未结 5 1143
谎友^
谎友^ 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:58

    Change the name of the ViewBag property to NOT match the name of the DropDownList. Use ViewBag.yearDropDownDD instead of ViewBag.yearDropDown.

    ALSO, you are creating the SelectList wrongly. You need to pass the selected value not the array item:

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

提交回复
热议问题