Cannot post to asp.net MVC controller -
here request definition:
params = typeof params !== "undefined" ? params : {}; var deferred = $q.defer(); var response = $http({ method: "post", datatype: "json", data: json.stringify(params), headers: { 'content-type': "application/json; charset=utf-8", }, url: 'apiurl' + 'mvccontrollername' + '/' });
and c#:
public class mvccontrollername : controller { public actionresult index(int? id = null) { .......
i not getting id parameter in controller class. it's null. parameter defined {id:1}. tested post chrome rest client same results.
any idea, what's wrong?
i see couple of issues here.
- first name of controller should follow convention of
somenamecontroller
, might want callmvccontrollernamecontroller
the url specify missing slash delimit between path , controller name:
url: 'apiurl' + '/' + 'mvccontrollername' + '/'
or simply:
url: 'apiurl/mvccontrollername/'
or more correctly, let mvc routing (as suggested @jamierees):
url: '@url.action("index","mvccontrollername")'
3. main issue however, post
ing data should calling get
parameter part of url. however, if need have in post
, need decorate parameter frombody
attribute:
public actionresult index([frombody]int? id = null)
Comments
Post a Comment