动态 JSON 对象未传递到 MVC 控制器

Dynamic JSON object is not passing to MVC Controller

单击按钮时,我得到 lineItems 作为 json 对象并将其传递给 MVC 操作方法。在 C# 中,我得到 lineItems count = 1 表示数组正在传递,但行项目内的值

如下图所示,我得到了所有值,但没有得到内部对象

  $('#btnSubmit').on('click', function (event) {
            event.preventDefault();

            var lineItems = [];
            $.each(extractGetSectionData('adults'), function (index, value) {
                lineItems.push(value);
            });
            $.each(extractGetSectionData('children'), function (index, value) {
                lineItems.push(value);
            });
            $.each(extractGetSectionData('seniors'), function (index, value) {
                lineItems.push(value);
            });

            console.log(lineItems);

            $.ajax({
                url: "/Home/CompleteOrder",
                dataType: "JSON",
                type: "POST",
                data: {
                    ApplicationId: '',
                    ApiKey: '',
                    SendTicketsSms: true,
                    FirstName: $("#field-billing-first-name").val(),
                    LastName: $("#field-billing-last-name").val(),
                    Address: '',
                    City: '',
                    State: '',
                    PostalCode: $("#field-billing-postal-code").val(),
                    Country: $("#field-payment-card-number").val(), //http://www.theodora.com/country_digraphs.html
                    Email: $("#field-delivery-email").val(),
                    Phone: '',

                    CcNumberEncrypted: $("#field-payment-card-number").val(),
                    CcExpiryMonthEncrypted: $("#field-payment-expires-on").val(), //"1"
                    CcExpiryYearEncrypted: $("#field-payment-expires-on-year").val(), //"2016"
                    CcCvvEncrypted: $("#field-payment-security-code").val(), //"123"
                    CcZipCodeEncrypted: "", //"76020"

                    LineItems: lineItems
                }
            }).done(function(data) {

                document.write("Success :" + data.Success + "<br>");
                document.write("ResponseCode :" + data.ResponseCode + "<br>");
                document.write("ResponseReason :" + data.ResponseReason + "<br><br><br>");

                document.write("OrderIdentifier :" + data.OrderIdentifier + "<br>");
                document.write("OrderNumber :" + data.OrderNumber + "<br>");
                document.write("GrandTotal :" + data.GrandTotal + "<br>");
            });


        });




function extractGetSectionData(slug) {

    var sectionId = 123; 

    var sectionRows = $(".form-section-passes").find("div[data-slug='" + slug + "']");

    var data = [];

    sectionRows.each(function () {
        var firstName = $(this).find("input[id*='[first-name]']").val();
        var lastName = $(this).find("input[id*='[last-name]']").val();
        var country = $(this).find("select[id*='[country]']").val();

        data.push({
            OpenTicketSectionId: sectionId, //Adult
            FirstName: firstName,
            LastName: lastName,
            CountryOfOrigin: country,
        });
    });

    return data;
}

C# 模型

 public class CompleteOrderRequestLineItem
    {
        public int OpenTicketSectionId { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string CountryOfOrigin { get; set; }
    }




     public class CompleteOrderRequest
        {
            public Guid ApiKey { get; set; }
            public Guid ApplicationId { get; set; }

            public bool SendTicketsSms { get; set; }

            public string FirstName { get; set; }
            public string LastName { get; set; }
            public string Phone { get; set; }
            public string Email { get; set; }
            public string Address { get; set; }
            public string City { get; set; }
            public string Country { get; set; }
            public string PostalCode { get; set; }
            public string State { get; set; }



            public string CcNumberEncrypted { get; set; }
            public string CcExpiryMonthEncrypted { get; set; }
            public string CcExpiryYearEncrypted { get; set; }
            public string CcCvvEncrypted { get; set; }
            public string CcZipCodeEncrypted { get; set; }

            public List<CompleteOrderRequestLineItem> LineItems { get; set; }
        }


        public class CompleteOrderRequestLineItem
        {
            public int OpenTicketSectionId { get; set; }
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public string CountryOfOrigin { get; set; }
        }

来自$.ajax

dataType (default: Intelligent Guess (xml, json, script, or html))

The type of data that you're expecting back from the server. If none is specified, jQuery will try to infer it based on the MIME type of the response (an XML MIME type will yield XML, in 1.4 JSON will yield a JavaScript object, in 1.4 script will execute the script, and anything else will be returned as a string)

如果您需要向服务器发送 JSON 数据,请尝试 contentType

contentType (default: 'application/x-www-form-urlencoded; charset=UTF-8')

When sending data to the server, use this content type. Default is "application/x-www-form-urlencoded; charset=UTF-8", which is fine for most cases. If you explicitly pass in a content-type to $.ajax(), then it is always sent to the server (even if no data is sent). As of jQuery 1.6 you can pass false to tell jQuery to not set any content type header.

试试 contentType: "application/json;charset=utf-8"JSON.stringify:

$.ajax({
            url: "/Home/CompleteOrder",
            contentType: "application/json;charset=utf-8"
            dataType: "JSON",
            type: "POST",
            data: JSON.stringify({
                ApplicationId: '',
                ApiKey: '',
                SendTicketsSms: true,
                FirstName: $("#field-billing-first-name").val(),
                LastName: $("#field-billing-last-name").val(),
                Address: '',
                City: '',
                State: '',
                PostalCode: $("#field-billing-postal-code").val(),
                Country: $("#field-payment-card-number").val(), //http://www.theodora.com/country_digraphs.html
                Email: $("#field-delivery-email").val(),
                Phone: '',

                CcNumberEncrypted: $("#field-payment-card-number").val(),
                CcExpiryMonthEncrypted: $("#field-payment-expires-on").val(), //"1"
                CcExpiryYearEncrypted: $("#field-payment-expires-on-year").val(), //"2016"
                CcCvvEncrypted: $("#field-payment-security-code").val(), //"123"
                CcZipCodeEncrypted: "", //"76020"

                LineItems: lineItems
            })
        })