How to redirect to action from JavaScript method?
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")";
Related videos on Youtube
![r.r](https://i.stack.imgur.com/IaHEi.jpg?s=256&g=1)
r.r
Updated on April 27, 2021Comments
-
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 over 13 yearsIs your DeleteJob method a HttpGet function ?
-
Patrick J Collins over 11 yearsHttp Get is meant to be idempotent. You should be using a Post or a Delete instead.
-
-
Antony Blazer over 13 yearstry this in IE? I have a problem with window.location.href
-
Peter Porfy over 13 yearswhich version? In IE6 it doesn't work, but IE6 is not a browser anymore.
-
Antony Blazer over 13 yearsSorry, i forgot that i didn't use this in ie because i need to know url referrer.
-
Arkiliknam about 12 yearsAlthough 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 over 11 yearsThis works only if your web application is in the root of the site, though.
-
camainc over 10 yearsThis 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 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 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 over 10 years@PeterPorfy ok! Thanks! you answered my question
-
Dovlet Mamenov over 7 yearsYou 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 over 7 yearsThumbs up for the asp routing-way