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

后端 未结 8 2029
盖世英雄少女心
盖世英雄少女心 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:03
    @Html.DropDownList("NewsItemId",ViewBag.NewsItemId as SelectList, string.Empty,
        new { @class = "form-control" })
    

    change to

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

    ViewBag.NewsItemIdList name changed. also change in controller.

    0 讨论(0)
  • 2021-02-18 19:09

    First of all, how do you populate ViewBag.NewsItemId.Items ? We don't see where the values come from.

    In order to make a selected value work, it must be of the same type as the elements of your collections. If item.NewsItemId is of type T, ViewBag.NewsItemId.Items must be of type IEnumerable<T> (or any other collection type implementing it). It does not seem to be the case, because you set NewsItemId as the selected value while your collection is based on NewsItemId.Items which may not be of the same type.

    0 讨论(0)
  • 2021-02-18 19:11

    I had the same problem. In the example below The variable ViewData["DATA_ACREDITO_MODELO_INTEGRADO"] has a SelectListItem list with a default selected value but such attribute is not reflected visually.

    // data 
            var p_estadoAcreditacion = "NO";
            var estadoAcreditacion = new List<SelectListItem>();
            estadoAcreditacion.Add(new SelectListItem { Text = "(SELECCIONE)"    , Value = " "    });
            estadoAcreditacion.Add(new SelectListItem { Text = "SI"              , Value = "SI"   });
            estadoAcreditacion.Add(new SelectListItem { Text = "NO"              , Value = "NO"   });
    
            if (!string.IsNullOrEmpty(p_estadoAcreditacion))
            {
                estadoAcreditacion.First(x => x.Value == p_estadoAcreditacion.Trim()).Selected = true;
            }
             ViewData["DATA_ACREDITO_MODELO_INTEGRADO"] = estadoAcreditacion;
    

    I solved it by making the first argument of DropdownList, different to the id attribute.

    // error:
    @Html.DropDownList("SELECT__ACREDITO_MODELO_INTEGRADO"
    , ViewData["DATA_ACREDITO_MODELO_INTEGRADO"] as List<SelectListItem>
    , new
    {
    id         = "SELECT__ACREDITO_MODELO_INTEGRADO"
    ...
    // solved :
    @Html.DropDownList("DROPDOWNLIST_ACREDITO_MODELO_INTEGRADO"
    , ViewData["DATA_ACREDITO_MODELO_INTEGRADO"] as List<SelectListItem>
    , new
    {
    id         = "SELECT__ACREDITO_MODELO_INTEGRADO"
    ...
    
    0 讨论(0)
  • 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" })
    
    0 讨论(0)
  • 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" })
    
    0 讨论(0)
  • 2021-02-18 19:21

    I tried lot of stuff but eventually got it working like this

    In controller

    int valittu_tila = 1;
    var tapahtumantilat = new List<SelectListItem>();
    
    tapahtumantilat.Add(new SelectListItem { Text = "Tapahtumaa kirjataan", Value = "0" });
    tapahtumantilat.Add(new SelectListItem { Text = "Odottaa jonossa", Value = "1"});
    tapahtumantilat.Add(new SelectListItem { Text = "Tapahtumaa käsitellään", Value = "2"});
    
    ViewBag.tilalista = new SelectList(tapahtumantilat, "Value", "Text", valittu_tila);
    

    And then in view I just put (I have the onchange event and class for dropdown too here but you can remove those if you want).

    @Html.DropDownList("tuki_tila", ViewBag.tilalista as SelectList, "-- valitse tila --", new { @onchange = "executeticketsearch();", @class = "haku_dropdowns" })
    

    (Sorry about finnish in the text values and variable names, but those shouldn't matter :D)

    0 讨论(0)
提交回复
热议问题