2017-02-16 57 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 ..................... –