SelectItem不选择项目

时间:2013-03-15 17:58:17

标签: c# asp.net-mvc asp.net-mvc-3 razor

查看(RelacionamentoConvidado.cshtml)

@Html.DropDownList("Foi_Emitido", (IEnumerable<SelectListItem>)ViewBag.Foi_Emitido, @SRSVP.Util.Constante.HTML_HELPER_DROPDOWN_EMPTY_VALUE, new { @class = "input-small" })

Controller(EventoConvidadoController)

ViewBag.Foi_Emitido = new SelectList(Common.SimNao(model.foi_emitido), "Value", "Text");

Common.cs(静态类)

public static List<SelectListItem> SimNao(object selectedItem)
{
    List<SelectListItem> _returnList = new List<SelectListItem>();
    SelectListItem _mList = new SelectListItem();

    _mList = new SelectListItem() { Text = "Sim", Value = "true", Selected = selectedItem == null ? false : selectedItem.ToString().Equals("true") };
    _returnList.Add(_mList);

    _mList = new SelectListItem() { Text = "Não", Value = "false", Selected = selectedItem == null ? false : selectedItem.ToString().Equals("false") };
    _returnList.Add(_mList);

    return _returnList;
}

加载页面时,选择项目不是从我的数据库返回的选择项目。

我该怎么做?

1 个答案:

答案 0 :(得分:0)

当选择列表和绑定变量共享相同的名称 - Foi_Emitido 时,通常会发生这种情况。尝试重命名ViewBag SelectList

@Html.DropDownList("Foi_Emitido", (IEnumerable<SelectListItem>)ViewBag.Foi_Emitido_SelectList, @SRSVP.Util.Constante.HTML_HELPER_DROPDOWN_EMPTY_VALUE, new { @class = "input-small" })

Controller(EventoConvidadoController)

ViewBag.Foi_Emitido_SelectList= new SelectList(Common.SimNao(model.foi_emitido), "Value", "Text");