2017-02-16 60 views
1

我有这个在我的控制器调用MVC控制器功能:C#如何使用JavaScript或jQuery的

$('#btnDelete').click(function (e) { 

}); 

如何从js文件调用控制器函数?

+2

包括'@ Html.AntiForgeryToken()'在您发布 –

+0

@PrashanthBenny形式如何,这是一个特定的防伪问题的重复,如果OP似乎有不知道如何首先发布到控制器,或者是否指定了正在使用的表单?这个问题是如何成为这个问题的?据我所知,我确定它是某种类型的复制品,但不是这个问题的复制品。 - 对不起,我错过了什么。 – Nope

+0

上述问题的答案似乎也回答了这个问题。也许我错了...... :) –

回答

1
$.post("Controller/CreateUser", dataToPost) 
      .done(function(response, status, jqxhr){ 
       // this is the "success" callback 
      }) 
      .fail(function(jqxhr, status, error){ 
       // this is the ""error"" callback 
      }); 

var data = { 
     username: $('#username').val().trim(), 
     password: $('#password').val() 
    }; 

$.ajax({ 
    type: "POST", 
    url: "Controller/CreateUser", 
    content: "application/json;", 
    dataType: "json", 
    data: JSON.stringify(data), 
    success: function(d) { 

    }, 
    error: function (xhr, textStatus, errorThrown) { 

    } 
}); 

PS:根据UserViewModel属性构成的数据对象。

+0

我打错误:POST本地主机:56110 /用户/控制器/ DeleteUser – Enix

+0

我离开控制器,因为我不知道你的控制器的名称。只需将其删除并放置正确的控制器名称 –

0

里面的按钮点击,执行Ajax请求

$('#btnDelete').click(function (e) { 
    $.ajax({ 
     type: "POST", 
     url: 'controller/DeleteUser', 
     dataType: "json", 
     success: function(data){ 
     //html content 
     }, 
    }); 
} 
+0

为什么要设置'data:“Json”'?也许你的意思是'dataType'。 'dataType'是您期望从服务器获得的数据类型。数据属性是你将发送给服务器的数据。 –

+1

对不起,它应该是数据类型 – kamprasad

+0

我打错误: POST HTTP http:// localhost:56110/user/controller/DeleteUser 404(未找到) – Enix

0

这很容易访问使用AJAX POST方法的任何控制器的方法。

正如我在这里按照选定的国家使用 “RegionesController”方法名“GETSTATES”也是在这里,我路过 CountryId获得国家按本ID获得的状态。

EX:

function GetStates() { 
    $.ajax({ 
     type: "POST", 
     async: false, 
     url: abp.appPath + 'Regiones/GetStates?CountryId=' + $("#ddlCountry").val(), 
     success: function (result) { 
      //return data or object 
     }, 
     error: function (err) { 
      abp.notify.info(err.statusText); 
     } 
    }); 
} 
+0

伟大的工作Arpit ..................... –