I am trying to send data from my Javascript to my post method controller. I have knockout as my method to bind from Javascript to my view.
This is my controller, and this is wher convocationData is null
public async Task<ActionResult> CreateConvocation(string convocationData)
{
string baseUrl = "http://localhost:8080";
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(baseUrl);
This is how I try to call the method using jQuery:
var tmpSession = self.addSession();
var tmpDescription = self.addDesc();
var convocationData = {
sessionCode: tmpSession,
description: tmpDescription
};
$.ajax({
url: '/Convocations/CreateConvocation',
type: 'POST',
dataType: 'json',
data: JSON.stringify(convocationData),
contentType: 'application/json',
success: function (data) {
self.Convocations.push(new Convocation(self.addSession(), self.addDesc()));
self.addDesc("");
self.addSession("");
},
error: function (err) {
alert(err.status + " : " + err.statusText);
}
});
convocationData(one called "sessionCode" and another called "description"), but your controller action is only accepting a 1 param named "convocationData". Your service-side action doesn't know or care that you called your javascript object varaiable... just its properties and values. Also, I don't think you need toJSON.stringifyyour data object.[HttpPost]and instead of adding the parameters 1 by 1 to the action method, create a model that matches the object your are posting from javascript (property names and data types). So your action method would look like this maybe:public async Task<ActionResult> CreateConvocation(ConvocationData model). Then just create that POCO class (ConvocationData) in C# with the correct properties