使用jquery异步调用视图中的控制器方法

时间:2012-11-12 14:42:32

标签: asp.net asp.net-mvc jquery

我在控制器中有两种方法:PageLoadPage会返回view,但Load会返回JsonResult。我也有jquery-functions的视图,我在那里调用我的方法。

它们按顺序执行但我想异步调用它们。我该怎么做?

function **LoadPage**(page, isnext) {
    $(':focus').blur();
    $('#loadingBlockElement').show();

    $.ajax({
        type: "GET",
        url:  '@Html.Raw(Url.Action("page", new { formId = Model.Params.FormId, userId = Model.Params.UserId, baseVersion = Model.Params.BaseVersion, stateName = Model.Params.StateName, stateVersion = Model.Params.StateVersion, stateFormId = Model.Params.StateFormId, baseFormId = Model.Params.BaseFormId }))'+'&page='+page+'&isnext='+isnext,         

        success: function (result) {     
            $('body').find('.ui-dialog').find('div').remove();        
            $('body').find('.ui-dialog').remove();      
            WE = null;
            $('#main').html(result);
            $('form').hide();
        }
    });
}

function **Load**() {        
    $.ajax({
        type: "POST",
        url: "@Url.Action("load")"+'?userId=@Model.Params.UserId'+'&formId=@Model.Params.FormId'+'&baseVersion=@Model.Params.BaseVersion'+'&stateFormId=@Model.Params.StateFormId'+'&baseFormId=@Model.Params.BaseFormId'+'&stateName=@Model.Params.StateName'+'&stateVersion=@Model.Params.StateVersion'+'&page='+$('form:first').attr('ID'),
        success: function (result) {..bla-bla-bla

控制器

负载:

public JsonResult Load(int formId, int baseVersion, string stateName, int stateVersion, string page, string userId, int stateFormId, int baseFormId)
{
     ...
}

网页:

public ActionResult Page(int formId, int baseVersion, string stateName, int stateVersion, string page, string userId, bool? isNext, int stateFormId, int baseFormId)
{
    ...
}

1 个答案:

答案 0 :(得分:1)

ajax次调用as the documentation shows上将$.ajax属性设置为 true ,然后随后调用每个函数:

$(document).ready(function() {
    LoadPage(page);
    Load();
});

function LoadPage(page) {
   $.ajax({
       type: "GET",
       async: true, // this is what you're missing
       url: "yoururl",
       success: function (result) {
           // handle success
       }
   });
}

function Load() {        
    $.ajax({
        type: "POST",
        async: true, // this is what you're missing
        url: "yoururl",
        success: function (result) {
            // handle success
        }
    });
}