RedirectToAction不是重定向 [英] RedirectToAction not redirecting

查看:377
本文介绍了RedirectToAction不是重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它开始在这里的一个按钮点击的.ascx。

It starts here in the .ascx on a button click.

<script type="text/javascript">

function acceptG(id) {
    jQuery.get('<%: Url.Action("Accept", "Gift") %>', { id: id });
}

</script>

那么,这里是控制器所在RedirectToAction是:

Then, here is the Controller is where the RedirectToAction is:

 [Authorize]
    public ActionResult Accept(long id)
    {
        var result = _gtService.AcceptG(id, User.UserID);
        if (result.Success)
            return RedirectToAction("Index", "Magnets");
        else 
        return View("InvalidG");             
    }

下面是该指数

 public ActionResult Index()
    {
        ViewData["Count"] = _souvenirService.GetDataForUser(User.UserID).Count();
        return View();
    }

使用菜单,打这个指数的页面出现时,成功有一点要记住的是,它确实是这样的工作。但使用RedirectToAction当它击中所有code,但不会重定向。任何帮助是AP preciated。

One thing to remember is when using the Menu and hitting this Index the page comes up successfully, so it does work. But when using the RedirectToAction it hits all the code but does not redirect. Any help is appreciated.

推荐答案

jQuery是不是魔术,它只是针对JavaScript的包装。当你在服务器上做一个重定向,这并不意味着您的浏览器(AJAX请求)将自动明白你的意思做什么。你必须告诉我们要重定向浏览器。我创建了非常小的系统,为您的需要,但你可以创建复杂的,根据你的应用需求。

Jquery is not magic, its just a wrapper for javascript. When you do a redirect on server that doesn't mean that your browser(ajax-request) will automatically understand what you mean to do. You have to tell browser that we want to redirect. I have created very tiny system for your need , but you can create complex one-as per your applications requirement.

<script type="text/javascript">

function acceptG(id) {
    jQuery.ajax({
          url:'<%: Url.Action("Accept", "Gift") %>',
          data:{ id: id },
          success:function(data)
          {
               if(data=='Redirect') 
               {
                    window.location='<%: Url.Action("Index", "Magnets") %>';
               }
               else
               {
                   //do something else
               }
          }
     });
}

</script>



 [Authorize]
    public ActionResult Accept(long id)
    {
        var result = _gtService.AcceptG(id, User.UserID);
        if (result.Success)
            if(Request.IsAjaxRequest())
            {
                  return Content('Redirect')
            }
            return RedirectToAction("Index", "Magnets");
        else 
        return View("InvalidG");             
    }

这篇关于RedirectToAction不是重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆