How to redirect to action from JavaScript method?

157,919

Solution 1

To redirect:

function DeleteJob() {
    if (confirm("Do you really want to delete selected job/s?"))
        window.location.href = "your/url";
    else
        return false;
}

Solution 2

function DeleteJob() {
    if (confirm("Do you really want to delete selected job/s?"))
        window.location.href = "/{controller}/{action}/{params}";
    else
        return false;
}

Solution 3

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

Solution 4

Maybe better to make an anchor with DeleteJob url instead of button?

<a href="<%=Url.Action("DeleteJob", "YourController", new {selectedObject="someObject"})%>" onclick="return DeleteJob()">Löschen</a>

and use your javascript you wrote already:

function DeleteJob() {

        if (confirm("Do you really want to delete selected job/s?"))
            return true;
        else
            return false;
    }

So if function return true - you will be redirected. If function return false - you still stay on the page.

Solution 5

Use the @Url.Action method. This will work and determines the correct route regardless of what IIS server you deploy to.

Example-

window.location.href="@Url.Action("Action", "Controller")";

so in the case of the Index action on the Home controller -

window.location.href="@Url.Action("Index", "Home")";
Share:
157,919

Related videos on Youtube

r.r
Author by

r.r

Updated on April 27, 2021

Comments

  • r.r
    r.r about 3 years

    I have an input type="button"

    <input type="button" name="DeleteJob" runat="server" value="Löschen" onclick="DeleteJob()" />
    

    and JavaScript method:

    function DeleteJob() {
    
        if (confirm("Do you really want to delete selected job/s?"))
            return true;
        else
            return false;
    }
    

    How can I instead return true, redirect to Action DeleteJob?

    [HttpGet]
    public ActionResult DeleteJob(string selectedObject)
    {
        return View();
    }
    
    • Bjarki Heiðar
      Bjarki Heiðar over 13 years
      Is your DeleteJob method a HttpGet function ?
    • Patrick J Collins
      Patrick J Collins over 11 years
      Http Get is meant to be idempotent. You should be using a Post or a Delete instead.
  • Antony Blazer
    Antony Blazer over 13 years
    try this in IE? I have a problem with window.location.href
  • Peter Porfy
    Peter Porfy over 13 years
    which version? In IE6 it doesn't work, but IE6 is not a browser anymore.
  • Antony Blazer
    Antony Blazer over 13 years
    Sorry, i forgot that i didn't use this in ie because i need to know url referrer.
  • Arkiliknam
    Arkiliknam about 12 years
    Although IE6 is not a browser anymore, its a pity many large companies still use it and require it to work with in house applications.
  • Alen Siljak
    Alen Siljak over 11 years
    This works only if your web application is in the root of the site, though.
  • camainc
    camainc over 10 years
    This only works if the Javascript function is embedded in the View itself, right? It won't work if your Javascript is located in a separate file.
  • Sumit Ramteke
    Sumit Ramteke over 10 years
    @PeterPorfy In addition to your answer, what would you do to send some get/post parameter to it as well
  • Peter Porfy
    Peter Porfy over 10 years
    @sumitramteke you cannot create a post request like this (this is actually rewriting the browser location which will result in a get request) but you can easily add get parameters: window.location.href = "your/url?key=value";
  • Sumit Ramteke
    Sumit Ramteke over 10 years
    @PeterPorfy ok! Thanks! you answered my question
  • Dovlet Mamenov
    Dovlet Mamenov over 7 years
    You can put link to your action inside a View inside <script> tags use global variable var LoadNewDocURL = '@Url.Action("Edit", "SaleDocuments")'; Then add your params to this variable.
  • Verbe
    Verbe over 7 years
    Thumbs up for the asp routing-way