How to redirect to action from JavaScript method?

前端 未结 8 1934
孤城傲影
孤城傲影 2020-12-25 11:12

I have an input type=\"button\"


相关标签:
8条回答
  • 2020-12-25 11:52
    function DeleteJob() {
        if (confirm("Do you really want to delete selected job/s?"))
            window.location.href = "/{controller}/{action}/{params}";
        else
            return false;
    }
    
    0 讨论(0)
  • 2020-12-25 11:52

    I struggled with this a little because I wanted to use Knockout to bind the button to the click event. Here's my button and the relevant function from inside my view model.

    <a class="btn btn-secondary showBusy" data-bind="click: back">Back to Dashboard</a>
    
    var vm = function () {
    ...
        self.back = function() {
            window.location.href = '@Url.Action("LicenseDashboard", "Application")';
        }
    }
    
    0 讨论(0)
  • 2020-12-25 11:59

    I wish that I could just comment on yojimbo87's answer to post this, but I don't have enough reputation to comment yet. It was pointed out that this relative path only works from the root:

            window.location.href = "/{controller}/{action}/{params}";
    

    Just wanted to confirm that you can use @Url.Content to provide the absolute path:

    function DeleteJob() {
        if (confirm("Do you really want to delete selected job/s?"))
            window.location.href = '@Url.Content("~/{controller}/{action}/{params}")';
        else
            return false;
    }
    
    0 讨论(0)
  • 2020-12-25 12:00

    (This is more of a comment but I can't comment because of the low reputation, somebody might find these useful)

    If you're in sth.com/product and you want to redirect to sth.com/product/index use

    window.location.href = "index";
    

    If you want to redirect to sth.com/home

    window.location.href = "/home";
    

    and if you want you want to redirect to sth.com/home/index

    window.location.href = "/home/index";
    
    0 讨论(0)
  • 2020-12-25 12:06

    To redirect:

    function DeleteJob() {
        if (confirm("Do you really want to delete selected job/s?"))
            window.location.href = "your/url";
        else
            return false;
    }
    
    0 讨论(0)
  • 2020-12-25 12:09

    Youcan either send a Ajax request to server or use window.location to that url.

    0 讨论(0)
提交回复
热议问题