c# - Call Controller Method using Ajax request -


please call method controller using ajax request, below code, error had returned says source of controller cannot found.

here ajax code

 function getservices() {     var e = document.getelementbyid("catagories");     var struser = e.options[e.selectedindex].value;     var id = e.options[e.selectedindex].id;  $.ajax({     url: "~/vascontroller/executevas/",     //url: '<%= url.action("getservices", "vas") %>',     type: 'post',     contenttype: 'application/json',     data: {"id": id},     success: function (result) {         alert(result);     } });  } 

and here controller method

 [webmethod]         public static string getservices(string id)         {             return id;          } 

kindly advice, still beginner in c# , mvc

in controller file

public class yourcontrollernamecontroller : controller {    [httppost]    public actionresult dosomething(int? id)    {     //your code     return view();    } } 

then in view

$.post('@url.action("dosomething","yourcontrollername")', { id: id }, function (data) {  }); 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -