My current task involves fetching information through an MVC controller by sending a JSON object as a parameter.
The method within the controller looks like this:
public ActionResult GetLatestInfoLogs(InfoLogUserJson invoker, InfoLogUserJson affected) {
// code
}
I have a model on the server side that appears as follows:
public class InfoLogUserJson {
public int id { get;set; }
public int personId { get;set;}
public int customerId { get;set; }
public int rootUserId { get;set; }
public string ip { get;set; }
public bool unknown { get;set; }
}
In addition, I have a client-side script structured in this manner:
var InfoLogUser = function () {
this.id = ko.observable(0);
this.personId = ko.observable(0);
this.rootUserId = ko.observable(0);
this.customerId = ko.observable(0);
this.unknown = ko.observable(false);
this.ip = ko.observable(null);
}
InfoLogUser.prototype = (function() {
return {
toJSON: function() {
return {
personId: this.getPersonId(),
rootUserId: this.getRootUserId(),
customerId: this.getCustomerId(),
id: this.getId(),
unknown: this.getUnknown(),
ip: this.getIp()
}
}
}
}());
Within a JavaScript view model, my attempt at integrating these components is as follows:
var infoLogUser = new InfoLogUser();
infoLogUser.personId(1234);
$.ajax({
url: '/Whatever/GetLatestInfoLogs',
data: {
invoker: JSON.stringify(infoLogUser.toJSON()),
affected: null
},
dataType: 'application/json; charset: utf-8',
type: 'GET',
success: function(_infoLogs) {
alert('yay');
}
});
Upon inspecting my network log, I observe the following Query String Parameters:
invoker:{"personId":1234,"rootUserId":0,"customerId":0,"id":0,"unknown":false,"ip":null} affected:
However, upon reaching the GetLatestInfoLogs method in the MVC controller, the invoker parameter consistently registers as null. If I omit the JSON.stringify from the ajax request, the invoker parameter is not null, but still lacks any assigned value.
I am currently puzzled by this behavior and would appreciate any insights or suggestions you may have regarding this issue. Thank you!