Search code examples
c#asp.netasp.net-mvc-5viewbagdropdownlistfor

DropDownList not selected in Edit page


I Created Controller with "MVC 5 Controller with views, using Entity Framework" and modify "DropDownList" code in "Edit" view to

@Html.DropDownListFor(model=>model.prdepno,(SelectList)ViewBag.prdepno, new { @class = "form-control" })

in Controller use ViewBag

personal personal = db.personals.Find(id);

ViewBag.prdepno = new SelectList(db.prdeps, "prdepno", "prdepdes", emp_map.prdepno);

no error ,but dropdownlist not selected

I lookup from Adding a css class to select using @Html.DropDownList() and other question try to do that, but It not work (for me)

I don't know to do (sorry, in my communication.)


Solution

  • I think you need to change the name of ViewBag Object

    personal personal = db.personals.Find(id);
    
    ViewBag.DwPrdeps= new SelectList(db.prdeps, "prdepno", "prdepdes", emp_map.prdepno);
    

    after that use ViewBag.DwPrdeps in your view

    @Html.DropDownListFor(model=>model.prdepno,(SelectList)ViewBag.DwPrdeps, new { @class = "form-control" })