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 }
);
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.
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");
}
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}
);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.