1

I have this at my controller:

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> DeleteUser(UserViewModel viewModel)
{
}

I have this at my cshtml:

<input type="button" id="btnDelete" value="Delete" />

I have this at my js file:

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

});

How do I call controller function from js file?

prtdomingo
  • 971
  • 4
  • 14
Enix
  • 71
  • 3
  • 10
  • 2
    include `@Html.AntiForgeryToken()` in the form that you are posting – Prashanth Benny Feb 16 '17 at 09:33
  • @PrashanthBenny How is this a duplicate of a specific antiforgery question if OP seems to have no idea how to post to a controller in the first place or ha sspecified a form is being used? How did the question become about that? I'm sure it is a duplicate of some sort but not of that question as far as I can see. - Sorry if I missed something. – Nope Feb 16 '17 at 09:43
  • The answers to the question mentioned above seems to answer this question too. maybe i am wrong... :) – Prashanth Benny Feb 16 '17 at 09:50
  • @PrashanthBenny Very indirectly as OP of other question already knew how to call actions from javascript and had a totally different issue. Assuming OP of this question is new to ajax and calling into controllers from JavaScript, given the question, the marked duplicate would be more than overwhelming...just saying, a more fitting duplicate would be more along the lines of ► [http://stackoverflow.com/questions/15162760/how-to-call-an-mvc-action-using-only-javascript](http://stackoverflow.com/questions/15162760/how-to-call-an-mvc-action-using-only-javascript) – Nope Feb 16 '17 at 10:22
  • @Fran but this one has more possibility of breaking because of this guy `[ValidateAntiForgeryToken]` right? But the question seems to be the other way! `Confused!` :D – Prashanth Benny Feb 16 '17 at 10:27

3 Answers3

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

or

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: compose the data object according to the UserViewModel properties.

Thiago Custodio
  • 17,332
  • 6
  • 45
  • 90
0

Inside the button click, execute ajax request

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

It's very easy to access any controller method using ajax post method.

As here I'm getting states as per selected country using 'RegionesController' method name 'GetStates' also here I'm passing CountryId to get states as per this 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);
        }
    });
}