以下是我正在处理的View
的html。它包含一个能够根据需要添加其他行的表。在返回此特定SalesOrder
时,该表最初会填充View
个对象。
@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 :(得分:3)
您的部分正在使用BeginCollectinItem()
添加名为"salesOrderTbl"
的前缀,并且为了绑定,POST方法中的参数名称必须匹配。
[HttpPost]
public ActionResult Print(List<SalesOrder> salesOrderTbl)
或者,保留现有方法参数名称并将部分视图代码更改为
@using (Html.BeginCollectionItem("salesOrders"))
{
....
}
另请注意,您的partial正在创建<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);
});
});