【问题标题】:Passing entire Model from a View back to the Controller将整个模型从视图传递回控制器
【发布时间】:2012-09-25 15:42:05
【问题描述】:

我有一份清单:

public class Items
{
    public String Nro_Item { get; set; }
    public Sucursal Sucursal { get; set; }
    public Areas Area { get; set; }
    public Sectores Sector { get; set; }
    public bool ID_Estado { get; set; }
}

我将它传递给这个视图:

@using ClientDependency.Core.Mvc
@model IEnumerable<TrackingOperaciones.Controllers.Items>

@{
    ViewBag.Title = "Ver Items";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<br/>
@using (Html.BeginForm("CargarRecibidos", "Recepcion", FormMethod.Post))
{    
<table class="table table-striped table-bordered table-condensed">
    <tr>
        <th>
            Numero
        </th>
        <th>
            Sucursal
        </th>
        <th>
            Area
        </th>
        <th>
            Sector
        </th>
    </tr>
  @foreach (var item in Model)
  {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Nro_Item)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Sucursal.descripcion)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Area.descripcion)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Sector.Descripcion)
        </td>
        <td>
            @Html.CheckBoxFor(modelItem => item.ID_Estado)
        </td>
    </tr>
  }
</table>
<p>
    <input class="btn" name="Guardar" type="submit" value="Guardar"/> |
    @Html.ActionLink("Volver al listado de Recepción", "Index")
</p>
}

直到一切都很好:现在的问题是在控制器中取回带有检查值的整个模型,以便像这样处理它:

[HttpPost]
 public ActionResult CargarRecibidos(IEnumerable<Items> items)
        {
            if (ModelState.IsValid)
            {
                //do something here
            }
            return RedirectToAction("Index");
        }
    }

但我很惨,因为“项目”在回发中返回为空

我猜表格有问题

@using (Html.BeginForm("CargarRecibidos", "Recepcion"))
{  
    <input class="btn" name="Guardar" type="submit" value="Guardar"/>
}

请帮帮我!

【问题讨论】:

    标签: asp.net-mvc view model controller postback


    【解决方案1】:

    使用 IEnumerable

    那么控制器输入参数将是List

    也许还可以考虑使用带有 JSON 对象的 AJAX POST 来代替表单提交(更现代/优雅),但提交也可以。

    【讨论】:

    • 好的,现在一切都是 IEnumerable 了,但还是同样的问题。
    • 您是否在浏览器开发人员工具中看到实际发布的内容?还可以尝试在您的 Html 表单助手中添加参数 FormMethod.Post。
    • 不行,我用的是chrome,不知道怎么看贴出来的数据。
    • 谷歌如何启动“开发者工具”(我认为是 F12)并查看“网络”选项卡..
    【解决方案2】:

    部分问题是它不会发布整个模型,因为只有复选框是表单值。我认为您需要在表单中添加一个额外的隐藏值来跟踪项目编号,然后在控制器中专门处理复选框值。项目编号附加到发布值中的“复选框”以保持它们的区别。

    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Nro_Item)
            @Html.Hidden("Nro_Item", item.Nro_Item)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Sucursal.descripcion)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Area.descripcion)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Sector.Descripcion)
        </td>
        <td>
            @Html.CheckBox("checkbox" + item.Nro_Item, item.ID_Estado)
        </td>
    </tr>
    

    在控制器中只绑定到数字项,所以它可以用来处理复选框:

        [HttpPost]
        public ActionResult CargarRecibidos(List<string> nro_item)
        {
    
             foreach (string item in nro_item)
             {
                var checkbox=Request.Form["checkbox" + item];
                if (checkbox != "false") // if not false then true,false is returned
                {
                    // Do the action for true...
                }
                else 
                {
                    // Do the action for false
                }
             }
            return View();
        }
    

    【讨论】:

    • 非常感谢!它正在工作,尽管将整个列表发送回控制器到数据库中会很好。
    • 没问题。从您的原始代码看来,唯一要编辑的更改是在复选框中。如果您想让所有这些都可编辑,您当然需要添加适当的编辑字段,或者至少作为隐藏字段。 Phil Haack 的一篇文章解决了这个问题,这可能很有用。 haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx
    猜你喜欢
    • 1970-01-01
    • 2013-11-20
    • 1970-01-01
    • 2017-11-27
    • 1970-01-01
    • 2014-06-12
    • 1970-01-01
    • 2021-02-24
    • 2013-07-08
    相关资源
    最近更新 更多