Html.DropDownList Selected Value Not Working (Using Constructor with IEnumerable)

后端 未结 8 2033
盖世英雄少女心
盖世英雄少女心 2021-02-18 19:02

I have an issue where the selected value is not working for the Html.DropDownList helper method. See below:

This is My Controller:

public ActionResult Ed         


        
8条回答
  •  说谎
    说谎 (楼主)
    2021-02-18 19:20

    You have the same problem here:

    DropDownListFor Not Selecting Value

    Problem is in your ViewBag property name. Because it is same as your property in Model it will not work. You should just change name of your ViewBag prop to something else, like:

    ViewBag.NewsItemList = new SelectList(ViewBag.NewsItemId.Items, "Id", "Name", item.NewsItemId);
    

    and on View

    @Html.DropDownList("NewsItemId",ViewBag.NewsItemList as SelectList, string.Empty,
                               new { @class = "form-control" })
    

提交回复
热议问题