1
我被困在這一個,有人可以解釋爲什麼行不綁定?MVC:複雜數組模型綁定失敗
我試過用JSON.stringify和各種組合提交。
我現在通過單元測試運行它,它仍然沒有約束力。
var formCollection = new NameValueCollection
{
{ "PurchaseOrders[0][Lines][0][Part]", "ABD33-360-2" },
{ "PurchaseOrders[0][Lines][0][PartDescription]", "CARRIAGE ASSEMBLY (MOVEABLE)" },
{ "PurchaseOrders[0][Lines][0][PartIss]", "C" },
{ "PurchaseOrders[0][Lines][0][Price]", "1060.06" },
{ "PurchaseOrders[0][Lines][0][PriceBreaks]", "null" },
{ "PurchaseOrders[0][Lines][0][Number]", "5" },
{ "PurchaseOrders[0][Lines][0][LeadTime]", "0" },
{ "PurchaseOrders[0][Lines][0][Unit]", "EACH" },
{ "PurchaseOrders[0][Narrative][0]", "a" },
{ "PurchaseOrders[0][QualityNarrative][]", "a" },
{ "PurchaseOrders[0][Agent]", "11" },
{ "PurchaseOrders[0][COSITE]", "0" },
{ "PurchaseOrders[0][TotalVAT]", "0" },
{ "PurchaseOrders[0][VATNo]", "null" },
};
var valueProvider = new NameValueCollectionValueProvider(formCollection, null);
var metadata = ModelMetadataProviders.Current.GetMetadataForType(null, typeof(PreDelivery));
var bindingContext = new ModelBindingContext {
ModelName = "",
ValueProvider = valueProvider,
ModelMetadata = metadata
};
var controllerContext = new ControllerContext();
var sut = new DefaultModelBinder();
// act
PreDelivery PD = (PreDelivery)sut.BindModel(controllerContext, bindingContext);
Console.WriteLine(string.Format("PreDelivery {0}", PD != null));
Console.WriteLine(string.Format("PD.PurchaseOrders {0}", PD.PurchaseOrders != null));
Console.WriteLine(string.Format("PD.PurchaseOrders[0] {0}", PD.PurchaseOrders[0] != null));
Console.WriteLine(string.Format("PD.PurchaseOrders[0].Lines {0}", PD.PurchaseOrders[0].Lines != null));
Console.WriteLine(string.Format("PD.PurchaseOrders[0].Lines {0}", PD.PurchaseOrders[0].Lines.Count.ToString()));
類 - (大量簡化了這個帖子):
public class Order : IOrder {
public string Agent { get; set; }
public string Buyer { get; set; }
public string Carrier { get; set; }
public string Contact { get; set; }
public string Currency { get; set; }
public string CusSupCode { get; set; }
public string CustomerName { get; set; }
public string CusSupRef { get; set; }
public string CusSupAddr { get; set; }
public int iDelAddr { get; set; }
public string DelAddr { get; set; }
public string InvAddr { get; set; }
public eInvoiceType InvoiceType { get; set; }
public List<Line> Lines { get; set; }
public string[] Narrative { get; set; }
public string[] QualityNarrative { get; set; }
public string Release { get; set; }
public string Region { get; set; }
public class Line {
public string CusSupPart { get; set; }
public decimal DiscountPC { get; set; }
public string DP { get; set; }
public bool IsFOC { get; set; }
public bool IsScheduled { get; set; }
public string OrderRef { get; set; }
public int OrderRefLine { get; set; }
public bool POA { get; set; }
public string Part { get; set; }
public string PartDescription { get; set; }
public string PartIss { get; set; }
public decimal Price { get; set; }
public string[] Narrative { get; set; }
public int Number { get; set; }
public int LeadTime { get; set; }
public string Unit { get; set; }
public DateTime? ReqDate { get; set; }
public DateTime SchdDate { get; set; }
public decimal UnitConv { get; set; }
}
public class HistoryEntry {
public string Narrative { get; set; }
public DateTime Timestamp { get; set; }
}
}