0

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.

Community
  • 1
  • 1
Carsten Franke
  • 1,649
  • 2
  • 13
  • 30
  • Just to make sure it is not Kendo UI MVC wapper issue with .Net Core, could you remove **DataSourceRequest**? Then just post those two arrays only. – Win Jul 28 '16 at 19:10

1 Answers1

1

Create a model class and 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
}
adem caglin
  • 22,700
  • 10
  • 58
  • 78
  • Forgive me this expression: But this sucks. It's working, indeed! But why do I have to do it this way now? Do you have a link to some documentation where this is explained? I've updated my post with another call which is working with another content type but without the additional model. Maybe you can shed some light on this. – Carsten Franke Jul 29 '16 at 07:15
  • Sorry, i don't know why can't you use array as a model. May be anyone else has information about this. I wonder too. – adem caglin Jul 29 '16 at 08:13
  • I accidentally stumbled upon the answer: "At most one parameter is allowed to read from the message body. \[..] The reason for this rule is that the request body might be stored in a non-buffered stream that can only be read once." (see http://www.asp.net/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api) Unfortunately, I can't test it anymore - we switched back to MVC 5 for now. – Carsten Franke Aug 26 '16 at 06:35