Html.DropDownList selected value not working using ViewBag

后端 未结 5 1128
谎友^
谎友^ 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 21:00

    The problem may also be the name, see here.

    In the Controller

    ViewBag.PersonList= new SelectList(db.Person, "Id", "Name", p.PersonId); 
    

    In the View

    @Html.DropDownList("PersonList",(SelectList)ViewBag.PersonList )
    

    This will not work, you have to change the name, so it's not the same.

    @Html.DropDownList("PersonList123",(SelectList)ViewBag.PersonList )
    

    So change yearDropDown and it Will Work for you.

    Best regards Christian Lyck.

提交回复
热议问题