MVC4:跳过ajax成功并从模型重定向到另一个视图

时间:2015-06-02 19:08:58

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

我有一个功能,根据条件我显示jQuery对话框。如果condition为false,则只需要通过刷新重新加载重定向到另一个视图。 它适用于jQuery对话框和加载局部视图。

但是当条件失败时,我是RedirectToAction(“Index”),它假设加载新鲜。但是在jQuery对话框中再次加载。这意味着Ajax回调成功回调。

我的问题是如何避免jquery ajax成功回调并完全跳过。

这是代码。

 $.ajax({
        url: 'SaveAddress',
        type: 'Post',
        cache: false,
        data: $("#frmAddress").serialize(),
        success: function (data) {

            $("#dialogPopSuggestions").dialog({
                autoOpen: false,
                height: 600,
                width: 500,
                modal: true,
                title: "Suggesion Addresses",
                //dialogClass: 'warning-dialog',
                open: function (event, ui) {
                    //Load the  action which will return 
                    // the partial view 
                    $(this).html(data);
                    event.preventDefault();
                    event.stopPropagation();
                }
            });
            $("#dialogPopSuggestions").dialog("open");
        },
        error: function (xhr, ajaxOptions, thrownError) {
            alert("error occured");
        }

  [HttpPost]
    public ActionResult SaveAddress(FormCollection col)
    {
           ....
           ....
            if (addresssuggestionList.Count == 0) //no suggestion
        {
            memberAddressDetails = memberAddress.AddAddress(memberId, memberAddressDetails);
        }
        else
        {
            addresssuggestionList.Add(memberAddressDetails);
            ViewBag.IsPrimaryCompany = primaryCompany;
            ViewBag.IsPrimaryAddress = mainAddress;
            return PartialView("_PopDivSuggestionAddress", addresssuggestionList); //Jquery dialog loads perfect
        }
        return RedirectToAction("Index");//here suppose fresh load index, not jQuery dialog.
    }

1 个答案:

答案 0 :(得分:0)

function openPopup(data)
{
     $("#dialogPopSuggestions").dialog({
           autoOpen: false,
           height: 600,
           width: 500,
           modal: true,
           title: "Suggesion Addresses",
           //dialogClass: 'warning-dialog',
           open: function (event, ui) {
                //Load the  action which will return 
                // the partial view 
                $(this).html(data);
                event.preventDefault();
                    event.stopPropagation();
           }
      });
      $("#dialogPopSuggestions").dialog("open");
}


$.ajax({
    url: '/Cntrllr/SaveAddress',
    type: 'Post',
    cache: false,
    data: $("#frmAddress").serialize(),
    success: function (data) {
         if(data ==null){
             location.href='/Cntrllr/Index';
         }
         else{
             openPopup(data);
         }

    },
    error: function (xhr, ajaxOptions, thrownError) {
        alert("error occured");
        location.href='/Cntrllr/Index';
    }

您的控制器将

[HttpPost]
public ActionResult SaveAddress(FormCollection col)
{   ....
    ....
    if (addresssuggestionList.Count == 0) //no suggestion
    {
        memberAddressDetails = memberAddress.AddAddress(memberId, memberAddressDetails);
        return null;
    }
    else
    {
        addresssuggestionList.Add(memberAddressDetails);
        ViewBag.IsPrimaryCompany = primaryCompany;
        ViewBag.IsPrimaryAddress = mainAddress;
        return PartialView("_PopDivSuggestionAddress", addresssuggestionList); //Jquery dialog loads perfect
    }        
}