我无法在控制器视图中使用DropDownList成功绑定ViewModel值

时间:2018-11-14 22:13:12

标签: c# asp.net-mvc asp.net-mvc-4 razor-2

我从ActionResult发送了一个ViewBag,并且视图用正确的数据填充了DropDownList,但是当视图开始(执行操作)或更改项目选择并接收空值时,DropDownList没有使用模型值选择项目在后期动作中。

如果有人可以告诉我a是否可以达到我的目标

这是我的示例代码 我的ViewModel

public class PartidaAgregarViewModel
{
    public PartidaAgregarViewModel() { }

    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public long InnerId { set; get; }

    public CN_PartidasCabeceraDTO Cabecera { set; get; }
    public List<CN_PartidasDetalleDTO> Detalle { set; get; }

}

这是我的行动

public ActionResult Agregar()
{
    var DocumentoPartida = GetDocumentOfSession() as PartidaAgregarViewModel;
    if (null == DocumentoPartida)
    {
        DocumentoPartida = new PartidaAgregarViewModel()
        {
            Cabecera = new CN_PartidasCabeceraDTO()
            {
                CN_FechaPartida = DateTime.Now,
                CN_CodigoPartida = "Nuevo doc",
                CN_CreditoPartida = 0,
                CN_DebitoPartida = 0,
                CN_DocumentosID = 3,
                CN_PeriodoID = 7
            },
            Detalle = new List<CN_PartidasDetalleDTO>()
        };
        DocumentoPartida.Detalle = GetListaDetallePartidaPrueba();
    }

    ViewBag.CN_CuentaId = db.CN_EmpresaCatalogo.ToList();
    ViewBag.Cuentas = db.CN_EmpresaCatalogo.ToList();
    ViewBag.CN_PeriodoID = new SelectList(GetPeriodosHabilitados(), "CN_PeriodoId", "CN_PeriodoDscr");
    ViewBag.CN_DocumentosID = new SelectList(db.CN_PartidasDocumento, "CN_DocumentosID", "CN_DocumentoDescribe");

    return View(DocumentoPartida);
}

这是我的查看代码

<div class="form-group">
                @Html.LabelFor(model => model.Cabecera.CN_PeriodoID, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.DropDownList("CN_PeriodoID", null, htmlAttributes: new { @class = "form-control" })                        
                    @Html.ValidationMessageFor(model => model.Cabecera.CN_PeriodoID, "", new { @class = "text-danger" })
                </div>
            </div>

0 个答案:

没有答案