使用ASP.Net MVC从JQuery发布中丢失上下文 [英] Losing context from JQuery post with ASP.Net MVC

查看:54
本文介绍了使用ASP.Net MVC从JQuery发布中丢失上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在页面上有一个JQuery对话框,它调用如下内容:

I have a JQuery dialog box on a page that calls something like this:

$.post("/MyController/MyAction", { myKey: key} //...

成功到达这里:

[HttpPost]
public ActionResult MyAction(int myKey)
{
   //do some stuff
   return RedirectToAction("AnotherAction");       
}

问题在于RedirectToAction对Web浏览器没有影响.我猜这是因为JQuery帖子有点在不同的踩"位置,所以它不知道将响应发送到哪里?如何让浏览器加载新的响应?

The problem is that the RedirectToAction has no effect on the webbrowser. I am guessing this is because the JQuery post is kinda on a different 'tread' so it doesn't know where to send the response? How do I get the browser to load the new response?

推荐答案

是的,$.post正在执行ajax发布,并且该操作将不会重定向.

Yes, the $.post is doing an ajax post and the Action will not redirect.

无论如何,如果为什么要重新命名,为什么不提交表单,而无需ajax.

If you are reditecting anyway, why not just do a form submit, no need for ajax.

或者,您可以执行$.get并返回JsonResult来指示成功/失败,然后使用jQuery/javascipt进行重定向.

Alternatively you could do a $.get and return a JsonResult to indicate success/failure and redirect using jQuery/javascipt.

更新

类似这样的东西:

您可以在其中设置OK或ERR,并在其中添加任何对象作为项目",这些对象可以在View上使用,也可以是错误消息,URL重定向到等

Where you can set OK or ERR as a result and add any object as the "item" which can be something to use on the View or an error message, url to redirect to etc

[HttpPost] 
public JsonResult MyAction(int myKey) 
{ 
   //do some stuff 
   return new JsonResult { Data = new { Item = item, Result = "OK/ERR" } };
} 

这篇关于使用ASP.Net MVC从JQuery发布中丢失上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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