2

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: My Customers Array in Model is NULL, all other Fields in my form are not posting back either

3
  • What error are you getting? Commented Dec 3, 2014 at 9:56
  • I am not getting an error, the Problem is my Model from jQuery is not binding to my MVC Controller Model Commented Dec 3, 2014 at 10:00
  • @DawoodAwan You can convert your array into json format. Commented Dec 3, 2014 at 10:04

1 Answer 1

3

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;
    };
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.