POST two arrays in JSON to ASP.NET Core MVC 6 -
foreword: have application mvc 4 , .net v4.6.1 , working charm. 1 editor template sends ajax request controller 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 this:
[acceptverbs(httpverbs.post)] public jsonresult effectiverights([datasourcerequest] datasourcerequest request, guid[] privileges, guid[] privilegegroups) { // stuff }
the payload of post request follows:
{"privileges":["d72c1162-0c3d-e611-953e-00155d9e5c08","e32c1162-0c3d-e611-953e-00155d9e5c08"],"privilegegroups":["bb2c1162-0c3d-e611-953e-00155d9e5c08"]}
whenever ajax request sent, variables privileges
, privilegegroups
have information client. yay!
let's problem. new application should use mvc 6 , .net core. according nuget, every library use up-to-date. javascript same. controller got attribute (it doesn't work acceptverbs
either):
[httppost] public jsonresult effectiverights([datasourcerequest] datasourcerequest request, guid[] privileges, guid[] privilegegroups) { // stuff }
the payload , request headers of both applications identical. whatever reason, variables privileges
, privilegegroups
never contain elements.
i've tried add [frombody]
did not either.
https://stackoverflow.com/a/38493849/4944034 had similar problem. sent 1 object, have two. , suggest solution did not work me.
what have change make work?
edit have similar on same page. data submitted component kendo. content type application/x-www-form-urlencoded
, payload looks this:
profileid=8f96c1bb-5c68-4071-a423-ab2a7ba8234f&selectedprivileges=1410335f-9e35-4454-a7e9-77c7d24bf5df&selectedgroups=60d0ec60-c820-47d7-acea-f4d57f221e5c
the controller able receive 2 arrays:
[httppost] public jsonresult privilegelistforuser([datasourcerequest]datasourcerequest request, guid[] selectedprivileges, guid[] selectedgroups) { // stuff }
may due defaultcontractresolver
setting in startup.cs
?
public void configureservices(iservicecollection services) { services.addmvc() .addjsonoptions(options => { options.serializersettings.contractresolver = new defaultcontractresolver(); }); services.addkendo(); }
best regards, carsten
ps: might have noticed using telerik's kendo. yes, using different versions in both applications. not see, how kendo should interfere here.
create model class , use it:
public class inputmodel { public guid[] privileges { get; set; } public guid[] privilegegroups { get; set; } } [httppost] public jsonresult effectiverights([datasourcerequest] datasourcerequest request, [frombody]inputmodel model) { // stuff }
Comments
Post a Comment