2012-09-25 51 views
1

我有一个列表:我将它传递给这一观点从View传递整个模型回Controller

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"); 
     } 
    } 

但是我坠落错误erably因为“项目”中的回传回来的空

我是猜的东西是错误的形式

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

请帮帮我!

回答

2

使用的IEnumerable

然后控制器输入参数将目录

或许也考虑代替形式的使用AJAX POST使用JSON对象提交(更现代/优雅的),但提供的也没关系。

+0

好了,现在一切都IEnumerable的,但还是同样的问题。 – Milo

+0

您是否在浏览器开发人员工具中看到实际正在发布的内容?也可以尝试在你的Html表单助手中添加参数FormMethod.Post。 –

+0

不,我正在使用铬,我不知道如何查看发布的数据。 – Milo

1

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

<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(); 
    } 
+0

非常感谢!它正在工作,但将整个列表发回控制器到数据库会很好。 – Milo

+0

没问题。从您的原始代码看起来好像唯一要编辑的更改是在复选框中。如果你想让它变得可编辑,你当然需要添加适当的编辑字段,或者至少作为隐藏字段。 Phil Haack有一篇文章解决了这个问题,可能有用。 http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx – Turnkey

相关问题