MVC Post动作没有从List <t>对象获取任何数据

时间:2016-08-13 21:06:20

标签: c# asp.net-mvc razor

我从List中接收数据时遇到问题,在Chrome中看到它看起来很好,因为它有所有值,但在Visual Studio Debug中它不正确,它会填充列表项,但不会填充值。我做错了什么?

型号:

public class PermisoModel
{
    public int Id;
    public string Name;
    public bool Checked;
}

public class ModuloModel
{
    public int Id;
    public string Name;
    public List<PermisoModel> Permisos { get; set; }
}

控制器:

public ActionResult Permisos(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        Usuario usuario = db.Usuario.FirstOrDefault(x => x.id_usuario == id && x.usu_estado == true);
        if (usuario == null)
        {
            return HttpNotFound();
        }
        List<ModuloModel> ModuloModel = db.Modulo.Where(mod => mod.mod_estado == true).Select(mod => new ModuloModel {
                Id = mod.id_modulo,
                Name = mod.mod_nombre,
                Permisos = db.Permiso.Where(per => per.id_modulo == mod.id_modulo && per.per_estado == true).Select(x => new PermisoModel { Id = x.id_permiso, Name = x.id_permiso + " - " + x.per_nombre, Checked = db.PermisoUsuario.Count(y => x.id_permiso == y.id_permiso && y.id_usuario == usuario.id_usuario)>0 }).ToList()
        }
        ).ToList();
        return View(ModuloModel);
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Permisos(int? id, List<ModuloModel> model)
    {

        return View(model);
    }

查看:

@model List<ModuloModel>
@using Data.Models
@using (Html.BeginForm())
{
    <p class="btn-group pull-right">
        <a href="@Url.Action("Index")" class="btn btn-default">
            <i class="glyphicon glyphicon-menu-left"></i>
            Volver
        </a>
        <button type="submit" class="btn btn-warning"><i class="glyphicon glyphicon-pencil"></i> Editar</button>
    </p>
    <hr style="clear:both;" />
    @Html.AntiForgeryToken()
    for (int x = 0; x < Model.Count; x++)
    {
        <fieldset class="form-horizontal">
            @Html.HiddenFor(model => model[x].Id)
            <legend>@Model[x].Name</legend>
            @for (int y = 0; y < Model[x].Permisos.Count; y++)
            {
                <div class="form-group">
                    <label>
                        @Html.HiddenFor(model => model[x].Permisos[y].Id)
                        @Html.CheckBoxFor(model => model[x].Permisos[y].Checked)
                        @Html.DisplayFor(model => model[x].Permisos[y].Name)
                    </label>
                </div>
            }
        </fieldset>
    }
}

提前致谢!

1 个答案:

答案 0 :(得分:1)

对于将发布的表单数据映射到HttpPost操作方法的参数的模型绑定器,viewmodel / dto classes属性应该是包含setget访问者的公共属性

您当前的代码没有set个访问者。因此,模型绑定器无法将值(从发布的表单数据)设置为它。

这应该有用。

public class PermisoModel
{
    public int Id { set; get; }
    public string Name { set; get; }
    public bool Checked { set; get; }
}

public class ModuloModel
{
    public int Id { set; get; }
    public List<PermisoModel> Permisos { get; set; }
}