从视图到控制器的模型为空

Model from view to controller comes null

我有一个调用部分视图的索引视图,我在其中列出了我的模型(一个 IEnumerable)并为 select 一些要更新的项目绘制了复选框。

我用 ajax 形式包装了部分视图,以发送模型(带有选中的复选框),但是当它到达控制器操作时 "Enviar",自定义对象参数出现空

我不知道为什么,根据一些帖子我的代码应该可以工作,拜托,我做错了什么?

索引视图

@model IEnumerable<beDGRAIC.T_Sedd_Cuadro_Envio_Empresa>
.....
@Html.Partial("_ListaResumenCarga", Model)
.....

_ListaResumenCarga 视图

@model IEnumerable<beDGRAIC.T_Sedd_Cuadro_Envio_EmpresaViewModel>

@using (Ajax.BeginForm("Enviar", "Upload", new { @id = "FormCabecera" },
new System.Web.Mvc.Ajax.AjaxOptions()
{
    HttpMethod = "POST",
    InsertionMode = System.Web.Mvc.Ajax.InsertionMode.Replace,
    UpdateTargetId = "SeccionListado",
    OnSuccess = "RefrescaListado"
}))
{
    <table>
        <tbody>
            @if (Model != null)
            {
                foreach (var item in Model)
                {
                    <tr>
                        <td class="text-center">@Html.EditorFor(modelItem => item.Seleccionado, new { @checked = "checked" })</td>
                        <td class="text-left">@Html.DisplayFor(modelItem => item.Categoria)</td>
                        <td class="text-left">@Html.DisplayFor(modelItem => item.Codigo)</td>
                        <td class="text-left">@Html.DisplayFor(modelItem => item.Nombre)</td>
                        <td class="text-center">@Html.DisplayFor(modelItem => item.Nro_Registros)</td>
                    </tr>
                    @Html.HiddenFor(modelItem => item.Seleccionado)
                    @Html.HiddenFor(modelItem => item.Id_Cuadro_Envio)
                }
            }
        </tbody>
    </table>
    <button type="submit">Enviar</button>
}

上传控制器

public class UploadController : Controller
{
    [HttpPost]
    public ActionResult Enviar(IEnumerable<T_Sedd_Cuadro_Envio_EmpresaViewModel> lT_Sedd_Cuadro_Envio_EmpresaViewModel)
    {
        ....
        return View("Envios", vT_Sedd_Envio_Empresa);
    }
}

型号Class

[DataContract]
[Serializable]
public partial class T_Sedd_Cuadro_Envio_EmpresaViewModel : BEPaginacion
{
    [DataMember]
    public bool Id { get; set; }
    [DataMember]
    public bool Seleccionado { get; set; }
    [DataMember]  
    public int Id_Cuadro_Envio { get; set; }  
    [DataMember]  
    public int Id_Envio_Empresa { get; set; }  
    [DataMember]  
    public string Categoria { get; set; }  
    .... // more properties
}

您在部分中使用 foreach 循环会生成重复的 id(无效的 html)和 name 属性,这意味着它无法绑定到集合。您需要为 T_Sedd_Cuadro_Envio_EmpresaViewModel 类型使用 for 循环或自定义 EditorTemplate。此外,您正在为 属性 Seleccionado

生成复选框和隐藏输入

编辑器模板 /Views/Shared/EditorTempplates/T_Sedd_Cuadro_Envio_EmpresaViewModel.cshtml

@model T_Sedd_Cuadro_Envio_EmpresaViewModel
<tr>
  <td>
    @Html.CheckBoxFor(m => m.Seleccionado)</td> // do not set the checked attribute!
    @Html.HiddenFor(m => m.Id_Cuadro_Envio) // inside a td element
  </td>
  <td>@Html.DisplayFor(m => m.Categoria)</td>
  .... // other properties to display/edit
</tr>

然后在部分

@model IEnumerable<beDGRAIC.T_Sedd_Cuadro_Envio_EmpresaViewModel>
@using (Ajax.BeginForm("Enviar", "Upload", ....))
{
  <table>
   <thead>
     .... // table headings
   </thead>
   <tbody>
     @EditorFor(m => m) // this will render all the rows
   </tbody>
  </table>
  <button type="submit">Enviar</button>
}

旁注:

  1. A html helper 绑定到 属性 的值(在你的例子中是 bool Categoria),这意味着如果 Categoria 的值是 true, 复选框将被选中,否则将被取消选中。不要 尝试设置 checked 属性,无论如何都不会 工作,因为你没有正确使用重载)
  2. <input> 不是 <tr> 元素的有效子元素
  3. 你给你起的名字classT_Sedd_Cuadro_Envio_EmpresaViewModel 建议它是一个视图模型,这显然不是(在我编辑之前 它包含 17 个属性,但您只在视图中使用了 6 个)。视图模型 在视图中表示您想要 dislay/edit 的内容 - 请参阅 什么是 中的视图模型 mvc(请保持简单,只有 EmpresaViewModelpublic ActionResult Enviar(IEnumerable<EmpresaViewModel> model)