for循环中的MVC Ajax表单仅适用于第一个循环项
我有一个页面,显示要开具发票的付款列表,付款被分组到客户中,每个组周围都有一个Ajax表单。当我在页面上提交第一组发票的第一张表单时,它会正确发回,并且我会将填充模型发送给我的控制器进行处理。如果我提交任何其他表单,表单会将所有数据发回(我可以使用firebug看到它发送了所有数据),但是调用controller方法时MVC不会填充我的模型,它只会返回为null 我的看法是:for循环中的MVC Ajax表单仅适用于第一个循环项,ajax,asp.net-mvc,forms,asp.net-ajax,Ajax,Asp.net Mvc,Forms,Asp.net Ajax,我有一个页面,显示要开具发票的付款列表,付款被分组到客户中,每个组周围都有一个Ajax表单。当我在页面上提交第一组发票的第一张表单时,它会正确发回,并且我会将填充模型发送给我的控制器进行处理。如果我提交任何其他表单,表单会将所有数据发回(我可以使用firebug看到它发送了所有数据),但是调用controller方法时MVC不会填充我的模型,它只会返回为null 我的看法是: @for (int i = 0; i < Model.InvoicingReportModels.Count();
@for (int i = 0; i < Model.InvoicingReportModels.Count(); i++)
{
@Html.EditorFor(m => m.InvoicingReportModels[i], "_InvoicingReportModel")
}
我的模型
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; }
}
我想出了解决办法。若要对缺少的数组元素使用模型绑定器,您需要为每个名为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")
}
}
for(int i=0;ipublic 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; }
}
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
@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")
}
}