简体   繁体   中英

How to send a DataSourceRequest and serialized form data to MVC controller via AJAX call

I am having an issue with posting form data and a DataSourceRequest to my controller method via Ajax. I am able to pass each object individually but when I try to pass both objects, my DataSourceRequest is always null.

Here is my js code:

function submitForm() {

    var grid = $('#denialGrid').data('kendoGrid');

    parameterMap = grid.dataSource.transport.parameterMap;

    var requestObject = parameterMap({
        sort: grid.dataSource.sort(), filter: grid.dataSource.filter(), group: grid.dataSource.group(), page: grid.dataSource.page(),
        pageSize: grid.dataSource.pageSize()
    });

    var formData = $('#BillingForm').serialize();
    $.ajax({
        type: "POST",
        url: '@Url.Action("SaveUpdate", "Home")',
        data: {requestObject,formData}, 
        success: alert(JSON.stringify(requestObject)),

    })
}

And here's my controller method

[HttpPost]
    public ActionResult SaveUpdate([DataSourceRequest] DataSourceRequest request, UpdateViewModel vm)
    {

        return null;
    }

I've tried:

data: {requestObject,formData},
data: [requestObject,formData],
data: requestObject, formData,
data: (requestObject,formData),
data: {request: requestObject, vm: formData},

Any help would be greatly appreciated

You should include formData inside requestObject, and only pass requestObject

  var requestObject = parameterMap({ sort: grid.dataSource.sort(), filter: grid.dataSource.filter(), group: grid.dataSource.group(), page: grid.dataSource.page(), pageSize: grid.dataSource.pageSize(), formData: $('#BillingForm').serialize() }); $.ajax({ type: "POST", url: '@Url.Action("SaveUpdate", "Home")', data: requestObject, success: alert(JSON.stringify(requestObject)), }) 

And I think serialize() will only return string. You need to convert it to object. Or you can try this

 [HttpPost] public ActionResult SaveUpdate([DataSourceRequest] DataSourceRequest request, string formData) { return null; } 

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