jQuery Window.Location.Href 或其他重定向 _self 的方法不起作用(MVC 视图)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17410700/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Window.Location.Href or other methods of redirecting _self not working (MVC View)
提问by shix
I'm sure there is probably a very simple explanation for this, but I have tried several methods to no avail.
我敢肯定对此可能有一个非常简单的解释,但是我尝试了几种方法都无济于事。
A little new to MVC but I've set up a controller with a conditional search param and now just need a way of passing it the parameter via querystring.
MVC 有点新,但我已经设置了一个带有条件搜索参数的控制器,现在只需要一种通过查询字符串将参数传递给它的方法。
Direcly navigating to:
直接导航到:
Collectors/Index?searchName=Tom
works perfectly, so I've set up a Textbox on the view to accept the parameter and am trying to redirect user to searchName= Input.
工作完美,所以我在视图上设置了一个文本框来接受参数,并试图将用户重定向到 searchName= Input。
The problem is that for some reason the window.location.href function is not redirecting the page at all. I've tried window.open method with target set to _self, and that didn't work - however using target _new or not specifying always works.
问题是由于某种原因 window.location.href 函数根本没有重定向页面。我尝试过将目标设置为 _self 的 window.open 方法,但这不起作用 - 但是使用目标 _new 或不指定总是有效。
Is there any reason why the function wouldn't work on the same window?
是否有任何原因为什么该功能不能在同一个窗口上工作?
The following produces an alert before, no redirect and no alert after:
以下在之前产生警报,没有重定向,之后没有警报:
$("#search").click(function () {
alert("before");
window.location.href("../Collector/Index?collectorName=Tom");
alert("after");
however this produces both alerts and the redirect (as well as search results)
但是这会产生警报和重定向(以及搜索结果)
$("#search").click(function () {
alert("before");
window.open("../Collector/Index?collectorName=Tom");
alert("after");
});
My controller action:
我的控制器动作:
public ActionResult Index(string collectorName)
{
var db = new CollectorsCRUDController();
var query = db.GetEXCEL_Collectors();
if (!String.IsNullOrEmpty(collectorName))
{
query = query.Where(c => c.CollectorName.Contains(collectorName));
}
return View(query);
}
Any tips would be awesome! Thank you!
任何提示都会很棒!谢谢!
Resolved
解决
For anyone else that's having a problem with this:
对于遇到此问题的其他任何人:
my problem was that I was using the wrong syntax to call the window.location method...
我的问题是我使用错误的语法来调用 window.location 方法......
window.location = URL
not
不是
window.location(URL);
the full code that worked for me
对我有用的完整代码
$("#search").click(function () {
var query = $("#searchName").val();
window.location = "/Collector/Index?collectorName=" + query;
});
回答by Alfie
Edited:
编辑:
window.location
/ window.location.href
and other variants require setting with the =
operator, rather than with parentheses (()
) such as used with window.open()
.
window.location
/window.location.href
和其他变体需要使用=
运算符进行设置,而不是使用括号 ( ()
),例如使用 with window.open()
。
Change: window.location.href("../Collector/Index?collectorName=Tom");
改变: window.location.href("../Collector/Index?collectorName=Tom");
To: window.location.href="../Collector/Index?collectorName=Tom";
到: window.location.href="../Collector/Index?collectorName=Tom";