47

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();
}
2
  • Is your DeleteJob method a HttpGet function ? Commented Mar 17, 2011 at 9:59
  • 1
    Http Get is meant to be idempotent. You should be using a Post or a Delete instead. Commented Nov 30, 2012 at 16:36

9 Answers 9

46

To redirect:

function DeleteJob() {
    if (confirm("Do you really want to delete selected job/s?"))
        window.location.href = "your/url";
    else
        return false;
}
Sign up to request clarification or add additional context in comments.

6 Comments

try this in IE? I have a problem with window.location.href
which version? In IE6 it doesn't work, but IE6 is not a browser anymore.
Sorry, i forgot that i didn't use this in ie because i need to know url referrer.
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.
@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";
|
37
function DeleteJob() {
    if (confirm("Do you really want to delete selected job/s?"))
        window.location.href = "/{controller}/{action}/{params}";
    else
        return false;
}

2 Comments

This works only if your web application is in the root of the site, though.
Thumbs up for the asp routing-way
17

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

2 Comments

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.
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.
12

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.

Comments

12

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

Comments

3

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

Comments

3

(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";

Comments

1

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

Comments

0

Here is another way. It isn't really that useful but showing for completeness:

Handle all your logic in your imported javascript file

function someFunction(){
  var x = $('#whoknows').val();
  var y = something;
  values = {
   property1 = x;
   property2 = y;
  };
  save(values);  <--- call the method in the view here
}

Then ONLY do the actual call to the controller from your view:

<script type="text/javascript">
    function save(values) {
        var url = '@Url.Action("SomeAction", "SomeController", new { _values_ = 1 })'
            .replace('_values_=1', new URLSearchParams(values));
        window.location.href = url;
    }
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.