DropDownListFor Not Selecting Value

后端 未结 11 1147
醉梦人生
醉梦人生 2020-11-29 18:14

I\'m using the DropDownListFor helper method inside of an edit page and I\'m not having any luck getting it to select the value that I specify. I noticed a similar question

11条回答
  •  萌比男神i
    2020-11-29 19:03

    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"
    

    ...

提交回复
热议问题