简体   繁体   English

使用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? 我的代码有什么问题,如何重定向到“详细信息”视图页面?

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);
}

This could be done by using a hidden variable in the view and then using that variable to post from the JavaScript code. 这可以通过在视图中使用隐藏变量,然后使用该变量从JavaScript代码发布来完成。

Here is my code in the view 这是我在视图中的代码

@Html.Hidden("RedirectTo", Url.Action("ActionName", "ControllerName"));

Now you can use this in the JavaScript file as: 现在,您可以在JavaScript文件中将其用作:

 var url = $("#RedirectTo").val();
 location.href = url;

It worked like a charm fro me. 它像我的魅力。 I hope it helps you too. 希望对您有帮助。

You can use: 您可以使用:

window.location.href = '/Branch/Details/' + id;

But your Ajax code is incomplete without success or error functions. 但是,没有成功或错误功能,您的Ajax代码是不完整的。

// in the HTML code I used some razor
@Html.Hidden("RedirectTo", Url.Action("Action", "Controller"));

// now down in the script I do this
<script type="text/javascript">

var url = $("#RedirectTo").val();

$(document).ready(function () {
    $.ajax({
        dataType: 'json',
        type: 'POST',
        url: '/Controller/Action',
        success: function (result) {
            if (result.UserFriendlyErrMsg === 'Some Message') {
                // display a prompt
                alert("Message: " + result.UserFriendlyErrMsg);
                // redirect us to the new page
                location.href = url;
            }
            $('#friendlyMsg').html(result.UserFriendlyErrMsg);
        }
    });
</script>
<script type="text/javascript">
    function lnkLogout_Confirm()
    {
        var bResponse = confirm('Are you sure you want to exit?');

        if (bResponse === true) {
            ////console.log("lnkLogout_Confirm clciked.");
            var url = '@Url.Action("Login", "Login")';
            window.location.href = url;
        }
        return bResponse;
    }

</script>

check the code below this will be helpful for you: 检查下面的代码,这将对您有所帮助:

<script type="text/javascript">
  window.opener.location.href = '@Url.Action("Action", "EventstController")', window.close();
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM