Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# web-api post to function with two parameters

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 }
            );
like image 333
Eyal Avatar asked Dec 16 '14 08:12

Eyal


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.


1 Answers

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.

like image 150
dotnetstep Avatar answered Oct 10 '22 07:10

dotnetstep