C# 使用Ajax请求调用控制器方法

C# 使用Ajax请求调用控制器方法,c#,ajax,asp.net-mvc-4,model-view-controller,C#,Ajax,Asp.net Mvc 4,Model View Controller,请帮助使用ajax请求从控制器调用方法,下面是我的代码,但返回的错误表示找不到控制器的源代码 这是我的ajax代码 function GetServices() { var e = document.getElementById("catagories"); var strUser = e.options[e.selectedIndex].value; var id = e.options[e.selectedIndex].id; $.ajax({ url:

请帮助使用ajax请求从控制器调用方法,下面是我的代码,但返回的错误表示找不到控制器的源代码

这是我的ajax代码

 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);
    }
});

}

好心的建议,我还是c#和MVC的初学者,在你的控制器文件中

public class YourControllerNameController : Controller
{
   [HttpPost]
   public ActionResult Dosomething(int? id)
   {
    //your code
    return View();
   }
}
那么在你看来,

$.post('@Url.Action("Dosomething","YourControllerName")', { id: id }, function (data) {

});

您必须执行以下操作: 1-用
[HttpPost]
标记装饰动作方法 2-删除Ajax URL的“controller”一词,它将是
'URL:“~/Vas/ExecuteVas/”

3-如果1和2不起作用,请尝试将Ajax URL放在不带
~/

的位置,而不带
MVC
,但显示
WebMethod
示例!另外,您的
uri
在javascript代码中不正确。如何在javascript代码中更正url?放弃web方法我正在使用的MVC此方法有何错误?用
[HttpGet]
标记GetServices()方法。这样就可以返回一条错误消息,服务器的响应状态为400(错误请求)。您的uri(使用纯MVC时),“/Controller/Action”(remove~)
$.post('@Url.Action("Dosomething","YourControllerName")', { id: id }, function (data) {

});