2012-09-25 52 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

相關問題