Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I want to redirect from one page to another page in asp.net MVC 3.0 using javascript/Jquery/Ajax. On button click event I have written javascript code like below.

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 button it is calling Details action inside BranchController but it doesn't return to Details view.

I didn't get any error or exception. It's showing status 200 OK in firebug. What is wrong in my code and How can I redirect to Details view page. Please help me.

share|improve this question

4 Answers

up vote 14 down vote accepted

You are not subscribing to any success callback in your $.post AJAX call. 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. You use AJAX only when you want to stay on the same page and update only a portion of it.

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. You should always be using url helpers when dealing with urls in an ASP.NET MVC application. So:

function foo(id) {
    var url = '@Url.Action("Details", "Branch", new { id = "__id__" })';
    window.location.href = url.replace('__id__', id);
}
share|improve this answer
1  
Thank you @Darin. I am using url helper as you suggested but its error on page because url is something like this localhost/… How to get proper Url from this helper. – Sharad Nov 16 '11 at 8:40
3  
@Sharad, is this in a separate javascript file? If it is you cannot use url helpers in it. You could define url as a global variable inside your view: <script type="text/javascript">var url = '@Url.Action("Details", "Branch", new { id = "__id__" })';</script>. And then you can use it in your separate javascript file. – Darin Dimitrov Nov 16 '11 at 8:42
1  
yes this is in a seperate JS file. I placed it in master page and now its working. Thanks a lot @Darin. – Sharad Nov 16 '11 at 8:49

you can use

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

but your ajax code is incomplete without success or error functions

share|improve this answer

This could be done by using a hidden variable in the view and then using that variable to post from the javascript.

Here is my code in the view

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

Now you can use this in the js file as

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

worked like charm fro me. Hope it helps you too.

share|improve this answer
<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>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.