jQuery Mobile的/ MVC:获取浏览器的URL与RedirectToAction改变 [英] jQuery Mobile/MVC: Getting the browser URL to change with RedirectToAction

查看:143
本文介绍了jQuery Mobile的/ MVC:获取浏览器的URL与RedirectToAction改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的第一个帖子...

My first post...

当我使用RedirectToAction在浏览器中的URL不会改变。我怎样才能做到这一点?

我切换到ASP.NET MVC 3.0后,使用Web表单10年以上(也使用jQuery移动)。我约8星期到它,经过几本书和淘谷歌回答,我来了干。

I'm switching over to ASP.NET MVC 3.0 (also using jQuery Mobile) after 10+ years using web forms. I'm about 8 weeks into it, and after several books and scouring Google for an answer, I'm coming up dry.

我在Global.asax中定义的单一路线:

I have a single route defined in Global.asax:

routes.MapRoute(
"Routes",
"{controller}/{action}/{id}",
new { controller = "Shopping", action = "Index", id = UrlParameter.Optional }

我与这些行动ShoppingController:

I have a ShoppingController with these actions:

public ActionResult Cart() {...}

public ActionResult Products(string externalId) {...}

[HttpPost]
public ActionResult Products(List<ProductModel> productModels)
{
    // do stuff
    return RedirectToAction("Cart");
}

当我做了GET和POST(与具有RedirectToAction后)的网址永远是:

The url when I do a get and post (with the post having the RedirectToAction) is always:

/Shopping/Products?ExternalId=GenAdmin

在帖子和RedirectToAction我想在浏览器中的网址更改为:

After the post and RedirectToAction I want the url in the browser to change to:

/Shopping/Cart

我试过重定向,以及RedirectToRoute但得到相同的结果。

I've tried Redirect, and RedirectToRoute but get the same results.

任何帮助将大大AP preciated。

Any help would be greatly appreciated.

[更新]
我发现jQuery Mobile的AJAX职位是这里的罪魁祸首。如果我关闭了jQuery Mobile的的AJAX它的工作原理。

[Update] I found that jQuery Mobile AJAX posts are the culprit here. If I turn off jQuery Mobile's AJAX it works.

<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
<script type="text/javascript">
    // do not handle links via ajax by default
    $(document).bind("mobileinit", function () { $.mobile.ajaxEnabled = false; });
</script>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0rc2/jquery.mobile-1.0rc2.min.css" />

上述脚本的顺序是重要的。我必须包括脚本jQuery的第一个,然后包括脚本禁用jQuery Mobile的的使用AJAX的,然后包括脚本jQuery Mobile的。

The ordering of the above scripts is important. I had to include the script to jQuery first, then include the script to disable jQuery Mobile's use of AJAX and then include the script to jQuery Mobile.

我还是想找到一种方法,使用AJAX和妥善有网址更新。或者至少能够调用的jQuery移动的加载消息(或烤我自己)。

I'd still like to find a way to use AJAX and have the url update properly. Or at the least be able to call jQuery Mobile's "loading" message (or bake my own).

推荐答案

我想我已经找到了答案。深藏在 jQuery Mobile的文档埋,有关于设置<$ C信息$ C>数据网址与数据角色=页面。当我这样做,我得到了很好的jQuery Mobile的AJAX的东西(网页加载信息,页面过渡),我得到在浏览器中正确更新的URL。

I think I've found an answer. Buried deep in the jQuery Mobile documentation, there is information about setting the data-url on the div with data-role="page". When I do this, I get the nice jQuery Mobile AJAX stuff (page loading message, page transitions) AND I get the url in the browser updated correctly.

从本质上讲,这是我正在做...

Essentially, this is how I'm doing it...

[HttpPost]
public ActionResult Products(...)
{
    // ... add products to cart
    TempData["DataUrl"] = "data-url=\"/Cart\"";
    return RedirectToAction("Index", "Cart");
}

然后我的页面布局上我有这样的....

Then on my layout page I have this....

<div data-role="page" data-theme="c" @TempData["DataUrl"]>

在我的 HttpPost 动作我现在设置了的TempData [DataUrl] 因此对于那些页面它被设置并填充布局页面上。 获取行动不设置到TempData [DataUrl]所以它不会在那些人口situtations布局页面上。

On my HttpPost actions I now set the TempData["DataUrl"] so for those pages it gets set and is populated on the layout page. "Get" actions don't set the TempData["DataUrl"] so it doesn't get populated on the layout page in those situtations.

这并不与此相当的工作的唯一一件事,就是当你右键点击...查看源文件......在浏览器中,HTML并不总是你的页面上,这是不不寻常的AJAX。

The only thing that doesn't quite work with this, is when you right-click... view source... in the browser, the html isn't always for the page you are on, which isn't unusual for AJAX.

这篇关于jQuery Mobile的/ MVC:获取浏览器的URL与RedirectToAction改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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