以下是我正在處理的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
合作,所以如果有人能指出我出錯的地方,我將不勝感激。
請檢查[此答案](https://stackoverflow.com/questions/23552931/pass-list-objects-with-mvc-razor)。您需要將SalesOrder的計數器傳遞給您的部分視圖,以便您可以創建正確的輸入文本框名稱以使MVC模型綁定正常工作。 – User3250
理解模型綁定的最簡單的方法是保持輸入名稱,就像在c#中訪問數組對象一樣,如'salesOrders [i] .SalesOrderId' – User3250
@ User3250如果包含我的SalesOrder對象的附加錶行被動態創建,這會影響你提出的解決方案嗎? – terbubbs