I am using ASP.NET Boilerplate template. I am having problems getting the server to receive the data I am sending from an abp.ajax post request (code shown below).
The console is logging the correct data (as shown in screenshot 1) but server is not returning the correct data. When I debug, the data of id 1000 is appearing as 0 on server even though console logs 1000 and that's the object sent.
Screenshot 1:
The Jquery:
$(document).on('click', '.default-customer-selection', function (e) {
e.preventDefault();
var info = {
toCustomerId: 1000 //Just trying to keep this simple for now
};
console.log(info);
abp.ajax({
url: abp.appPath + 'Portal/Catalog/SwitchCustomer',
data: JSON.stringify(info),
success: function (data) {
console.log('Data Returned: ' + data.personId);
}
});
});
Model/DTO Input for Switch Customer:
using System;
using System.Collections.Generic;
using System.Text;
namespace MySolution.Catalog.Dtos
{
public class SwitchCustomerInput
{
public long ToCustomerId { get; set; }
}
}
My Controller Function:
public class CatalogController : MySolutionControllerBase
{
[HttpPost]
public JsonResult SwitchCustomer(SwitchCustomerInput input)
{
return Json(new { PersonId = input.ToCustomerId });
}
}
I am following the guide and other pre-built examples I see in the project that are working properly in the solution already: https://aspnetboilerplate.com/Pages/Documents/v1.5.2/Javascript-API/AJAX
Am I missing a step? Any help is greatly appreciated!
abp.ajax
gets options as an object. You can pass any parameter that is valid in jQuery's $.ajax method. There are some defaults here: dataType is 'json', type is 'POST' and contentType is 'application/json' (So, you call JSON.stringify to convert javascript object to JSON string before sending to the server).
You need to add [FromBody]
attribute to the parameter of the action like below :
[HttpPost]
public JsonResult SwitchCustomer([FromBody]SwitchCustomerInput input)
{
return Json(new { PersonId = input.ToCustomerId });
}