2017-07-27 87 views
2

以下是我正在處理的View的html。它包含一個能夠根據需要添加額外行的表。在返回這個特定的View後,表格最初填充了一個SalesOrder對象。將對象從PartialView傳遞到控制器

@model List<Project.Models.SalesOrder> 

@{ 
    Layout = "~/Views/Shared/_Layout.cshtml"; 
} 

@using (Html.BeginForm("Print", "Project", FormMethod.Post)) 
{ 
    <div class="col-lg-12"> 
     <table class="table table-striped"> 
      <thead> 
      <tr> 
       <th>SO#</th> 
       <th>Cust#</th> 
       <th>DC</th> 
       <th>Stop</th> 
       <th>Addr L1</th> 
       <th>Addr L2</th> 
       <th>City</th> 
       <th>State</th> 
       <th>Zip</th> 
       <th>Ref 1</th> 
       <th>Ref 2</th> 
       <th>Pcs</th> 
      </tr> 
      </thead> 
      <tbody> 
      @foreach (var salesOrder in Model) 
      { 
       @Html.Partial("_SalesOrderRecord", salesOrder) 
      } 
      </tbody> 
     </table> 
    </div> 
    <div class="col-lg-12"> 
     <button type="button" class="btn btn-primary" id="add">Add</button> 
     <button type="button" class="btn btn-danger" id="delete">Remove Last Record</button> 
     <button style="float: right;" type="submit" class="btn btn-danger" id="print">Print Label(s)</button> 
    </div> 
} 

一旦添加按鈕被點擊時,下面的腳本被稱爲和PartialView是該表的foreach中添加的。

var url = '@Url.Action("Add", "ProjectController")'; // adjust to suit 
$('#add').click(function() { 
    $.get(url, function (response) { 
     $('<tr />').html(response).appendTo(tableBody); 
    }); 
}); 

下面是PartialView

@model Project.Models.SalesOrder 

@using (Html.BeginCollectionItem("salesOrderTbl")) 
{ 
    <tr id="addRow" class="form-group"> 
     @Html.HiddenFor(m => m.SalesOrderId) 
     <td>@Html.TextBoxFor(m => m.SalesOrderNumber, new {@class="form-control", @style="height: auto; width: 5em;"})</td> 
     <td>@Html.TextBoxFor(m => m.CustId, new { @class = "form-control", @style = "height: auto; width: 5em;" })</td> 
     <td>@Html.TextBoxFor(m => m.Location, new { @class = "form-control", @style = "height: auto; width: 5em;" })</td> 
     <td>@Html.TextBoxFor(m => m.DeliveryCompanyName, new { @class = "form-control", @style = "height: auto; width: 5em;" })</td> 
     <td>@Html.TextBoxFor(m => m.AddressL1, new { @class = "form-control", @style = "height: auto; width: 5em;" })</td> 
     <td>@Html.TextBoxFor(m => m.AddressL2, new { @class = "form-control", @style = "height: auto; width: 5em;" })</td> 
     <td>@Html.TextBoxFor(m => m.City, new { @class = "form-control", @style = "height: auto; width: 5em;" })</td> 
     <td>@Html.TextBoxFor(m => m.State, new { @class = "form-control", @style = "height: auto; width: 5em;" })</td> 
     <td>@Html.TextBoxFor(m => m.Zip, new { @class = "form-control", @style = "height: auto; width: 5em;" })</td> 
     <td>@Html.TextBoxFor(m => m.Reference1, new { @class = "form-control", @style = "height: auto; width: 5em;" })</td> 
     <td>@Html.TextBoxFor(m => m.Reference2, new { @class = "form-control", @style = "height: auto; width: 5em;" })</td> 
     <td>@Html.TextBoxFor(m => m.Pieces, new { @class = "form-control", @style = "height: auto; width: 5em;" })</td> 
    </tr> 
} 

下一步我試圖完成是通過使用打印按鈕張貼SalesOrder對象我ProjectController

下面是ProjectController方法,我想發佈:

[HttpPost] 
public ActionResult Print(List<SalesOrder> salesOrders) 
{ 
    //do things 
} 

不幸的是,這不是爲我工作和salesOrder列表對象總是空。我相信我誤解了如何與PartialView合作,所以如果有人能指出我出錯的地方,我將不勝感激。

+0

請檢查[此答案](https://stackoverflow.com/questions/23552931/pass-list-objects-with-mvc-razor)。您需要將SalesOrder的計數器傳遞給您的部分視圖,以便您可以創建正確的輸入文本框名稱以使MVC模型綁定正常工作。 – User3250

+0

理解模型綁定的最簡單的方法是保持輸入名稱,就像在c#中訪問數組對象一樣,如'salesOrders [i] .SalesOrderId' – User3250

+0

@ User3250如果包含我的SalesOrder對象的附加錶行被動態創建,這會影響你提出的解決方案嗎? – terbubbs

回答

3

您的部分內容是使用BeginCollectinItem()添加名爲"salesOrderTbl"的前綴,並且爲了綁定,POST方法中的參數名稱必須匹配。

[HttpPost] 
public ActionResult Print(List<SalesOrder> salesOrderTbl) 

另外,保留現有方法的參數名稱和局部視圖代碼更改爲

@using (Html.BeginCollectionItem("salesOrders")) 
{ 
    .... 
} 

還請注意,您的部分是創建一個<tr>元素,所以你並不需要內括起來另一個<tr>元素用於動態添加新行。它應該只是

var tableBody = $('#...'); // give your tbody an id 
var url = '@Url.Action("Add", "ProjectController")'; // adjust to suit 
$('#add').click(function() { 
    $.get(url, function (response) { 
     $(tableBody').append(response); 
    }); 
}); 
+0

感謝您的幫助!像魅力一樣工作。 – terbubbs

相關問題