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

后端 未结 8 2074
盖世英雄少女心
盖世英雄少女心 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: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();
            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
    , new
    {
    id         = "SELECT__ACREDITO_MODELO_INTEGRADO"
    ...
    // solved :
    @Html.DropDownList("DROPDOWNLIST_ACREDITO_MODELO_INTEGRADO"
    , ViewData["DATA_ACREDITO_MODELO_INTEGRADO"] as List
    , new
    {
    id         = "SELECT__ACREDITO_MODELO_INTEGRADO"
    ...
    

提交回复
热议问题