MVC:复杂数组模型绑定失败

时间:2014-11-04 16:13:39

标签: asp.net-mvc model-binding

我坚持这个,有人可以解释为什么线条不会绑定?

我尝试使用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; }
        }       
    }

1 个答案:

答案 0 :(得分:0)

数组绑定的预期格式为Property[0].Key,但您使用的是Property[0][Key]

请改为尝试:

{ "PurchaseOrders[0].Lines[0].Part", "ABD33-360-2" },
{ "PurchaseOrders[0].Lines[0].PartDescription", "CARRIAGE ASSEMBLY (MOVEABLE)" },
{ ... }