C # web-api post for working with two parameters

I am trying to publish two parameters for the following function, but I am unable to get to the function:

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

This is how I write:

 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 send only one parameter, it goes well and I can achieve 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"); } 

It's also good:

 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 } ); 
+5
source share
3 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 several parameters are not resolved or problematic due to the type of stream.

+3
source

try under the 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"); } 
0
source

Change the route configuration to accept two parameters: either update or add a new 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} ); 
0
source

Source: https://habr.com/ru/post/1209206/


All Articles