jquery - C# web-api post to function with two parameters -
i trying post 2 parameters following function dont manage reach function:
public void setshopsubcategories([frombody]string userid, int []subcategories ) { }
this how 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 post 1 parameter goes , can reach 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 1 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 link : http://www.asp.net/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api
you find multiple parameter not allowed or problematic due type of stream.
Comments
Post a Comment