简体   繁体   中英

Posting Array and Form Data to Controller - MVC Ajax

I've got an array of Objects in jQuery:

function customersList() {
    this.selectedCustomers = [];
}

function customerObject(customerId, bookingId) {
    this.customerId = customerId;
    this.bookingId = bookingId;
}

I need to post this to my Controller:

[HttpPost]
public ActionResult CreateMultipleCasesFormPost(CreateMultipleCasesModel model)
{
    return PartialView("_CreateMultipleCases", model);
 }

My ViewModel:

public class CreateMultipleCasesModel
{
    [Display(Name = "Selected Customers")]
    public List<CustomerList> Customers { get; set; }

I need to pass the Array from jQuery and the Data from this Form to my Controller (My View Model contains other properties):

$('#createMultipleCasesForm')

This is my Post Form jQuery Code:

$('#createMultipleCasesBtn').click(function () {
            var btn = $(this);
            var mUrl = btn.data('actionurl');

            var formModel = $('#createMultipleCasesForm').serializeArray();
            var customerList = customersList.selectedCustomers();

            var requestData = {
                model: formModel,
                Customers: customerList
            };

            var sData = JSON.stringify(requestData);

            $.ajax({
                url: mUrl,
                type: 'POST',
                dataType: 'json',
                contentType: 'application/json; charset=utf-8',
                data: sData,
                success: function (response) {

                    debugger;

                },
                error: function (response) {
                    $('#ErrorMessage').html('<span class="icon black cross"></span>' + response.Message);
                }
            });

        });

My Model from jQuery is not Binding either the Array of Customer Objects or the Form, What am I doing wrong here?

EDIT

What happens when I post Back my Form: 我在模型中的客户数组为NULL,我表单中的所有其他字段也不回发

I found a solution this did the trick for me:

$('#createMultipleCasesBtn').click(function () {
        var btn = $(this);
        var mUrl = btn.data('actionurl');

        var formModel = $('#createMultipleCasesForm').serializeObject();

        formModel['Customers'] = customersList.selectedCustomers;

        var sData = JSON.stringify(formModel);

        $.ajax({
            url: mUrl,
            type: 'POST',
            dataType: 'json',
            contentType: 'application/json; charset=utf-8',
            data: sData,
            success: function (response) {

            },
            error: function (response) {
                $('#ErrorMessage').html('<span class="icon black cross"></span>' + response.Message);
            }
        });

    });

This Function Below Used from Answer Here: Convert form data to JavaScript object with jQuery

$.fn.serializeObject = function () {
        var o = {};
        var a = this.serializeArray();
        $.each(a, function () {
            if (o[this.name] !== undefined) {
                if (!o[this.name].push) {
                    o[this.name] = [o[this.name]];
                }
                o[this.name].push(this.value || '');
            } else {
                o[this.name] = this.value || '';
            }
        });
        return o;
    };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM