c# - No action was found on the controller that matches the request -
sorry lame question. i've read similar questions , still can't resolve issue.
i'm getting 'no action found on controller matches request' error when calling ajax:
$.ajax({ url: '/api/toyedit/post/', datatype: "json", type: "post", contenttype: 'application/json; charset=utf-8', data: json.stringify({toyid: 1, toy: 'asd'}), async: true, processdata: false, cache: false, success: function (data) { alert(data); }, error: function (xhr) { alert(xhr.statustext); } })
controller:
public class toyeditcontroller : apicontroller { [system.web.mvc.httpget] public edittoyviewmodel get(int? toyid) { var model = new edittoyviewmodel(); if (toyid.hasvalue) { model.toy = data.gettoy(toyid.value); } model.companies = data.getcompanies().tolist(); return model; } [system.web.mvc.httppost] [system.web.mvc.actionname("post")] public actionresult post(int? toyid, string toy) { var = toy; /*new task<int>(() => db.context.toys.firstasync().id);*/ return new emptyresult(); } }
routing:
routes.maproute( name: "webapi", url: "api/{controller}/{action}/{id}", defaults: new { id = urlparameter.optional } ); routes.maproute( name: "default", url: "{controller}/{action}/{id}", defaults: new { controller = "home", action = "index", id = urlparameter.optional } );
what's wrong code?
update 1:
ok, works when i'm using next code:
public class toy { public int? toyid {get; set;} public string toy {get; set;} } [system.web.mvc.httppost] [system.web.mvc.actionname("post")] public actionresult post(toy toy) { // necessary codes }
but how pass few primitive vars?
try following: first, create toy
class
public class toy { public int? toyid {get; set;} public string toy {get; set;} }
then use following...
[system.web.mvc.httppost] [system.web.mvc.actionname("post")] public actionresult post(toy toy) { // necessary codes }
you can take here more information...
Comments
Post a Comment