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.

  1. first name of controller should follow convention of somenamecontroller, might want call mvccontrollernamecontroller
  2. 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, posting 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

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

Nuget pack csproj using nuspec -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -