6

I am trying to post two parameters to the following function but I dont manage to reach the function:

public void SetShopSubCategories([FromBody]string userId, int []subCategories )
{

}

This is how I post:

var subCategories = [ 1, 2, 3, 4, 5];
var userId = "123";

            $.ajax({
                type: "POST",
                url: "/Category/SetShopSubCategories/",
                contentType: 'application/json; charset=utf-8',
                data: JSON.stringify(userId, subCategories),
                success: function () {
                    alert("OK");
                },
                error: function () {
                    alert("error");
                }

When I post only with one parameter it goes well and I can reach the function:

public void SetShopSubCategories([FromBody]string userId )
{

}

var userId = "123";

        $.ajax({
            type: "POST",
            url: "/Category/SetShopSubCategories/",
            contentType: 'application/json; charset=utf-8',
            data: JSON.stringify(userId, subCategories),
            success: function () {
                alert("OK");
            },
            error: function () {
                alert("error");
            }

This one also goes well:

public void SetShopSubCategories( int []subCategories )
{

}

var subCategories = [ 1, 2, 3, 4, 5]; 

            $.ajax({
                type: "POST",
                url: "/Category/SetShopSubCategories/",
                contentType: 'application/json; charset=utf-8',
                data: JSON.stringify(subCategories),
                success: function () {
                    alert("OK");
                },
                error: function () {
                    alert("error");
                }

My RoutConfig:

 routes.IgnoreRoute("{resource}.axd/{*pathInfo}");


            GlobalConfiguration.Configuration.Routes.MapHttpRoute(
                name: "SetCategories",
                routeTemplate: "{controller}/{action}"
            );


            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );

3 Answers 3

3

Model

public class Mymodel
    {
        public string UserId { get; set; }        
        public int[] subCategories { get; set; }
    }

Controller Action

[HttpPost]
        public void SetShopSubCategories([FromBody]Mymodel model)
        {

        }

Ajax Call:

var subCategories = [1, 2, 3, 4, 5];
var userId = "123"
$.ajax({
    type: "POST",
    url: "/api/Values",
    contentType: "application/json; charset=utf-8",  
    data: JSON.stringify({ userid: userId, subCategories: subCategories }),
    success: function () {
        alert("OK");
    },
    error: function () {
        alert("error");
    }
});

Here is the link : http://www.asp.net/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api

you will find that multiple parameter not allowed or problematic due to type of stream.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, this is what I did.
If you have to support multiple than you have to build many thing from custom FromBody to how it binds and even you have to store your stream is custom buffer and all that.
0

try below code and also add dataType: 'json'

 $.ajax({
            type: "POST",
            url: "/Category/SetShopSubCategories",
            contentType: 'application/json; charset=utf-8',
            data: JSON.stringify({ userId  : userId, subCategories : subCategories}),
            dataType: 'json',
            success: function () {
                alert("OK");
            },
            error: function () {
                alert("error");
            }

2 Comments

Still cannot reach the function.
why have you put [FromBody] here? @Eyal
0

Change your route config to accept two parameters : either update or add anew route with a different name

routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}/{category}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional, category= UrlParameter.Optional}
            );

1 Comment

Still cannot reach the function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.