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

后端 未结 8 2024
盖世英雄少女心
盖世英雄少女心 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:16

    I did not like the fact that you can use

    @Html.DropDownList("NewsItemID") 
    

    and it works. Then you need to add the 'class' attribute and suddenly it breaks. I saw one place working and another not working using the syntax

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

    When I researched this, I determined the ViewBag.NewsItemId was actually a null in the view. So a working syntax, with the Model and ViewBag fields having the same name is

    @Html.DropDownList("NewsItemId", null, string.Empty,
                           new { @class = "form-control" })
    

提交回复
热议问题