我有一个页面显示要开票的付款清单,付款分组到客户中,每个组周围都有一个Ajax表单。当我在页面上为第一组发票提交第一个表单时,它会正确回发,我会将填充模型发送给我的控制器进行处理。如果我提交任何其他表单,表单回复所有数据(我可以使用firebug看到它全部被发送)但MVC在调用控制器方法时没有填充我的模型,它只是返回为null。
我的观点:
@for (int i = 0; i < Model.InvoicingReportModels.Count(); i++)
{
@Html.EditorFor(m => m.InvoicingReportModels[i], "_InvoicingReportModel")
}
_InvoiceReportMode.cshtml
@using (Ajax.BeginForm(new AjaxOptions() { HttpMethod = "POST", OnSuccess = "InvoiceSaved", OnFailure="InvoiceSaveFailed" }))
{
@Html.HiddenFor(m => m.DisplaySummary)
@Html.HiddenFor(m => m.MasterAccountID)
<div class="settings_bordered_box" style="margin-bottom: 20px;">
<div class="settings">
<table>
<tr>
<th>
@Html.DisplayFor(m => m.DisplaySummary)
<span class="settings_box_divider">| </span>
<span>@Html.Raw(Model.Payments.Count().ToString()) Invoices</span>
<span class="settings_box_divider">| </span>
@Html.DisplayFor(m => m.TotalPrice)
</th>
</tr>
</table>
</div>
<div class="settings">
<table>
<tr>
<th>
<input type="checkbox" id="@Html.IdForModel()_chkAll" class="selectAll" /></th>
<th>PaymentID</th>
<th>Date</th>
<th>Total Price</th>
<th>Payment Method</th>
<th>Invoice Number</th>
<th> </th>
</tr>
@for (int i = 0; i < Model.Payments.Count; i++)
{
Model.Payments[i].InvoiceNumberFieldID = Html.IdFor(m => m.Payments[i].InvoiceNumber).ToString();
<tr>
<td style="text-align: center; vertical-align: middle;">@Html.CheckBoxFor(m => m.Payments[i].Selected) @Html.HiddenFor(m => m.Payments[i].SingleSaveSelected)</td>
<td style="text-align: center; vertical-align: middle;">@Html.DisplayFor(m => m.Payments[i].DisplayPaymentID) @Html.HiddenFor(m => m.Payments[i].PaymentID) @Html.HiddenFor(m => m.Payments[i].PaymentType)</td>
<td style="text-align: center; vertical-align: middle;">@Html.DisplayFor(m => m.Payments[i].Date)</td>
<td style="text-align: center; vertical-align: middle;">@Html.DisplayFor(m => m.Payments[i].TotalPrice)</td>
<td style="text-align: center; vertical-align: middle;">@Html.DisplayFor(m => m.Payments[i].PaymentMethod)</td>
<td style="text-align: center; vertical-align: middle;">@Html.EditorFor(m => m.Payments[i].InvoiceNumber) @Html.HiddenFor(m => m.Payments[i].InvoiceNumberFieldID)</td>
<td style="text-align: center; vertical-align: middle;">
<input type="submit" name="submit" class="coloured_button" value="Save" onclick="SetSingleSave('@Html.IdFor(m => m.Payments[i].SingleSaveSelected)'); return true;" />
<input type="button" class="coloured_button" value="Details" />
</td>
</tr>
}
<tr>
<td style="text-align: center; vertical-align: middle;"> </td>
<td style="text-align: center; vertical-align: middle;"> </td>
<td style="text-align: center; vertical-align: middle;"> </td>
<td style="text-align: center; vertical-align: middle;"> </td>
<td style="text-align: center; vertical-align: middle;"> </td>
<td style="text-align: center; vertical-align: middle;">@Html.EditorFor(m => m.InvoiceNumber)</td>
<td style="text-align: center; vertical-align: middle;">
<input type="submit" name="submit" class="coloured_button" value="Save Selected" />
</td>
</tr>
</table>
</div>
</div>
}
我的控制器
[HttpPost]
public ActionResult Index(InvoicingModel model, string submit)
{
... Do controller stuff here
}
我的模特
public class InvoicingModel
{
public List<InvoicingReportModel> InvoicingReportModels { get; set; }
public InvoicingModel()
{
InvoicingReportModels = new List<InvoicingReportModel>();
}
}
public class InvoicingReportModel
{
public int MasterAccountID { get; set; }
public string DisplaySummary { get; set; }
public string InvoiceNumber { get; set; }
[DataType(DataType.Currency)]
public double TotalPrice
{
get
{
if (Payments != null && Payments.Count > 0)
{
return Payments.Sum(p => p.TotalPrice);
}
else
{
return 0.0;
}
}
}
public List<PaymentListModel> Payments { get; set; }
public InvoicingReportModel()
{
Payments = new List<PaymentListModel>();
}
}
public class PaymentListModel
{
public int PaymentID { get; set; }
public string DisplayPaymentID { get; set; }
[DataType(DataType.Currency)]
public double TotalPrice { get; set; }
public string PaymentMethod { get; set; }
public string InvoiceNumber { get; set; }
public bool Selected { get; set; }
public bool SingleSaveSelected { get; set; }
public DateTime Date { get; set; }
public string InvoiceNumberFieldID { get; set; }
public PaymentType PaymentType { get; set; }
}
第一个表单回发了一个InvoicingModel,其中填充了一个InvoicingReportModel,并且所有数量的PaymentListModel都已完全填充。如果它提交第二个或以后的表单,控制器将获得一个InvoicingModel,该InvoicingModel对于InvoicingReportModel列表为null。
我已经尝试更改控制器,以便它接受一个InvoicingReportModel(因为这就是表单所在的那个)但是我只是为所有内容都为null。
不确定还有什么可以尝试。
如果有帮助的话,那就是从页面上提交第3张表格发送的帖子数据的萤火虫捕获。
Parametersapplication/x-www-form-urlencoded
InvoicingReportModels[2].... 1316 - Some Thing Here
InvoicingReportModels[2].... 123
InvoicingReportModels[2].... 1316
InvoicingReportModels[2]....
InvoicingReportModels[2].... InvoicingReportModels_2__Payments_0__InvoiceNumber
InvoicingReportModels[2].... 13276
InvoicingReportModels[2].... Standard
InvoicingReportModels[2].... true
InvoicingReportModels[2].... false
InvoicingReportModels[2].... False
InvoicingReportModels[2]....
InvoicingReportModels[2].... InvoicingReportModels_2__Payments_1__InvoiceNumber
InvoicingReportModels[2].... 13298
InvoicingReportModels[2].... Standard
InvoicingReportModels[2].... true
InvoicingReportModels[2].... false
InvoicingReportModels[2].... False
X-Requested-With XMLHttpRequest
submit Save Selected
Source
submit=Save+Selected&InvoicingReportModels%5B2%5D.DisplaySummary=1316+-+Some+thing+Here&InvoicingReportModels%5B2%5D.MasterAccountID=1316&InvoicingReportModels%5B2%5D.Payments%5B0%5D.Selected=true&InvoicingReportModels%5B2%5D.Payments%5B0%5D.Selected=false&InvoicingReportModels%5B2%5D.Payments%5B0%5D.SingleSaveSelected=False&InvoicingReportModels%5B2%5D.Payments%5B0%5D.PaymentID=13276&InvoicingReportModels%5B2%5D.Payments%5B0%5D.PaymentType=Standard&InvoicingReportModels%5B2%5D.Payments%5B0%5D.InvoiceNumber=&InvoicingReportModels%5B2%5D.Payments%5B0%5D.InvoiceNumberFieldID=InvoicingReportModels_2__Payments_0__InvoiceNumber&InvoicingReportModels%5B2%5D.Payments%5B1%5D.Selected=true&InvoicingReportModels%5B2%5D.Payments%5B1%5D.Selected=false&InvoicingReportModels%5B2%5D.Payments%5B1%5D.SingleSaveSelected=False&InvoicingReportModels%5B2%5D.Payments%5B1%5D.PaymentID=13298&InvoicingReportModels%5B2%5D.Payments%5B1%5D.PaymentType=Standard&InvoicingReportModels%5B2%5D.Payments%5B1%5D.InvoiceNumber=&InvoicingReportModels%5B2%5D.Payments%5B1%5D.InvoiceNumberFieldID=InvoicingReportModels_2__Payments_1__InvoiceNumber&InvoicingReportModels%5B2%5D.InvoiceNumber=123&X-Requested-With=XMLHttpRequest
答案 0 :(得分:0)
我制定了解决方案。要使用缺少数组元素的模型绑定器,您需要为名为Index的每个数组元素设置一个隐藏字段。将表单移动到顶层视图中,并为每个迭代添加一个隐藏的Index元素,如下所示,并且全部工作。
@for (int i = 0; i < Model.InvoicingReportModels.Count; i++ )
{
using (Ajax.BeginForm(new AjaxOptions() { HttpMethod = "POST", OnSuccess = "InvoiceSaved", OnFailure = "InvoiceSaveFailed" }))
{
@Html.Hidden("InvoicingReportModels.Index", i)
@Html.EditorFor(m => Model.InvoicingReportModels[i], "_InvoicingReportModel")
}
}