How to Call Controller Actions using JQuery in ASP.NET MVC

后端 未结 5 2078
粉色の甜心
粉色の甜心 2020-11-29 05:28

I\'ve been reading on this for a while and found that you can call a controller action by using:

$.ajax(\"MyController/MyAction\", function(data) {
    alert         


        
5条回答
  •  野性不改
    2020-11-29 06:00

    You can start reading from here jQuery.ajax()

    Actually Controller Action is a public method which can be accessed through Url. So any call of an Action from an Ajax call, either MicrosoftMvcAjax or jQuery can be made. For me, jQuery is the simplest one. It got a lots of examples in the link I gave above. The typical example for an ajax call is like this.

    $.ajax({
        // edit to add steve's suggestion.
        //url: "/ControllerName/ActionName",
        url: '<%= Url.Action("ActionName", "ControllerName") %>',
        success: function(data) {
             // your data could be a View or Json or what ever you returned in your action method 
             // parse your data here
             alert(data);
        }
    });
    

    More examples can be found in here

提交回复
热议问题