Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
229 views
in Technique[技术] by (71.8m points)

c# - 使用JavaScript / jQuery重定向到ASP.NET MVC中的另一个页面(Redirecting to another page in ASP.NET MVC using JavaScript/jQuery)

I want to redirect from one page to another page in ASP.NET MVC 3.0 using JavaScript/jQuery/Ajax.(我想使用JavaScript / jQuery / Ajax从ASP.NET MVC 3.0中的一页重定向到另一页。)

On button click event I have written JavaScript code like below.(在按钮单击事件中,我已经编写了如下的JavaScript代码。) function foo(id) { $.post('/Branch/Details/' + id); } My controller code is like this:(我的控制器代码是这样的:) public ViewResult Details(Guid id) { Branch branch = db.Branches.Single(b => b.Id == id); return View(branch); } When I click on a button it is calling the Details action inside BranchController, but it doesn't return to the Details view.(当我单击一个按钮时,它正在BranchController内调用Details动作,但不会返回到Details视图。) I didn't get any error or exception.(我没有收到任何错误或异常。) It's showing status 200 OK in Firebug .(在Firebug中显示状态200 OK。) What is wrong in my code and how can I redirect to the Details view page?(我的代码有什么问题,如何重定向到“详细信息”视图页面?)   ask by translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are not subscribing to any success callback in your $.post AJAX call.(您没有在$ .post AJAX调用中订阅任何成功回调。)

Meaning that the request is executed, but you do nothing with the results.(这意味着该请求已执行,但是您对结果不执行任何操作。) If you want to do something useful with the results, try:(如果您想对结果做一些有用的事情,请尝试:) $.post('/Branch/Details/' + id, function(result) { // Do something with the result like for example inject it into // some placeholder and update the DOM. // This obviously assumes that your controller action returns // a partial view otherwise you will break your markup }); On the other hand if you want to redirect, you absolutely do not need AJAX.(另一方面,如果要重定向,则绝对不需要AJAX。) You use AJAX only when you want to stay on the same page and update only a portion of it.(仅当您希望停留在同一页面上并仅更新其中一部分时,才使用AJAX。) So if you only wanted to redirect the browser:(因此,如果您只想重定向浏览器:) function foo(id) { window.location.href = '/Branch/Details/' + id; } As a side note: You should never be hardcoding urls like this.(附带说明:绝对不要这样对URL进行硬编码。) You should always be using url helpers when dealing with urls in an ASP.NET MVC application.(在ASP.NET MVC应用程序中处理URL时,应始终使用URL帮助器。) So:(所以:) function foo(id) { var url = '@Url.Action("Details", "Branch", new { id = "__id__" })'; window.location.href = url.replace('__id__', id); }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...