我有一个显示待开票付款清单的页面,付款分组为客户,并且每个组都有一个Ajax表单。当我在第一组发票的页面上提交第一个表单时,它将正确回发,并将填充模型发送到我的控制器进行处理。如果我提交任何其他表单,表单会回传所有数据(我可以使用firebug查看它的全部数据),但是MVC在调用控制器方法时没有填充我的模型,它只是返回null。MVC for循环中的Ajax表单仅适用于第一个循环项目
笔者认为:
@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)
@Html.DisplayFor(m => m.DisplaySummary) | @Html.Raw(Model.Payments.Count().ToString()) Invoices | @Html.DisplayFor(m => m.TotalPrice) |
---|
PaymentID | Date | Total Price | Payment Method | Invoice Number |
---|
@for (int i = 0; i < Model.Payments.Count; i++)
{
Model.Payments[i].InvoiceNumberFieldID = Html.IdFor(m => m.Payments[i].InvoiceNumber).ToString();
@Html.CheckBoxFor(m => m.Payments[i].Selected) @Html.HiddenFor(m => m.Payments[i].SingleSaveSelected)@Html.DisplayFor(m => m.Payments[i].DisplayPaymentID) @Html.HiddenFor(m => m.Payments[i].PaymentID) @Html.HiddenFor(m => m.Payments[i].PaymentType)@Html.DisplayFor(m => m.Payments[i].Date)@Html.DisplayFor(m => m.Payments[i].TotalPrice)@Html.DisplayFor(m => m.Payments[i].PaymentMethod)@Html.EditorFor(m => m.Payments[i].InvoiceNumber) @Html.HiddenFor(m => m.Payments[i].InvoiceNumberFieldID)}
@Html.EditorFor(m => m.InvoiceNumber)}
我的控制器
[HttpPost]
public ActionResult Index(InvoicingModel model, string submit)
{
... Do controller stuff here
}
我的模型
public class InvoicingModel
{
public List InvoicingReportModels { get; set; }
public InvoicingModel()
{
InvoicingReportModels = new List();
}
}
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 Payments { get; set; }
public InvoicingReportModel()
{
Payments = new List();
}
}
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全部填充。如果它提交第2个或更多的表单,则控制器获取InvoicingReportModel列表为空的InvoicingModel。
我已经尝试更改控制器,以便它接受一个InvoicingReportModel(因为这是什么形式)左右,但然后我什么都没有。
不知道还有什么可以尝试的。
如果有帮助,这里是从提交页面上的第三个表单发送的发布数据的萤火虫捕获。
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
2014-03-05
Lepon