foreword: I have an application with MVC 4 and .NET v4.6.1 and is working like a charm. One editor template sends an AJAX request to the controller to get a list of things:
function showEffectiveRights(e) {
$.ajax({
contentType: "application/json",
data: JSON.stringify({
privileges: $("#AssignedPrivileges").getKendoMultiSelect().value(),
privilegeGroups: $("#AssignedGroups").getKendoMultiSelect().value()
}),
dataType: "json",
success: function (data) {
// Stuff
},
error: function (data) {
showResponseMessages(data);
},
type: "POST",
url: '@Url.Action("EffectiveRights", "User")'
});
}
The controller looks like this:
[AcceptVerbs(HttpVerbs.Post)]
public JsonResult EffectiveRights([DataSourceRequest] DataSourceRequest request, Guid[] privileges, Guid[] privilegeGroups)
{
// Stuff
}
The payload of the POST request is as follows:
{"privileges":["d72c1162-0c3d-e611-953e-00155d9e5c08","e32c1162-0c3d-e611-953e-00155d9e5c08"],"privilegeGroups":["bb2c1162-0c3d-e611-953e-00155d9e5c08"]}
Whenever the AJAX request is sent, the variables privileges
and privilegeGroups
have the information from the client. Yay!
Let's get to the problem. My new application should use MVC 6 and .NET Core. According to NuGet, every library I use is up-to-date. The JavaScript is exactly the same. The controller only got another attribute (it doesn't work with AcceptVerbs
either):
[HttpPost]
public JsonResult EffectiveRights([DataSourceRequest] DataSourceRequest request, Guid[] privileges, Guid[] privilegeGroups)
{
// Stuff
}
The payload and the request headers of both applications are identical. But for whatever reason, the variables privileges
and privilegeGroups
never contain any elements.
I've tried to add [FromBody]
but this did not help either.
https://stackoverflow.com/a/38493849/4944034 had a similar problem. But he sent only one object, I have two. And the suggest solution did not work for me.
What do I have to change to make this work?
EDIT
I have something similar on the very same page. The data are submitted by a component from Kendo. The content type is application/x-www-form-urlencoded
and the payload looks like this:
profileId=8f96c1bb-5c68-4071-a423-ab2a7ba8234f&selectedPrivileges=1410335f-9e35-4454-a7e9-77c7d24bf5df&selectedGroups=60d0ec60-c820-47d7-acea-f4d57f221e5c
The controller is very well able to receive those two arrays:
[HttpPost]
public JsonResult PrivilegeListForUser([DataSourceRequest]DataSourceRequest request, Guid[] selectedPrivileges, Guid[] selectedGroups)
{
// Stuff
}
May this be due to the DefaultContractResolver
I am setting in Startup.cs
?
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc()
.AddJsonOptions(options =>
{
options.SerializerSettings.ContractResolver = new DefaultContractResolver();
});
services.AddKendo();
}
Best regards, Carsten
PS: You might have noticed that I am using Telerik's Kendo. Yes, I am using different versions in both applications. But I do not see, how Kendo should interfere here.