How to redirect to different Url when button is click?

22,301

Solution 1

I think your form action method is overriding your button action method. You can use jquery click event like below.

$(document).read(function () {

    $("#btnId").click(function (event) {
        event.preventDefault();
        var url = '@Url.Action("Edit", "Order", new { id = "ID" })';
        window.location.href = url;
        });
});

Hope this helps.

Solution 2

Your form submit button will be calling a controller method

then in your method

poblic ActionResult Yourmethod()
{
//after your operations

return RedirectToAction("controllerName", ActionName, parameter if any);
 //this methods will load a different view from a differnt controller

}
Share:
22,301
Lakhae
Author by

Lakhae

Updated on July 09, 2022

Comments

  • Lakhae
    Lakhae almost 2 years

    I have a form

     <form id="Print" action="/Order/Print" method="POST" enctype="application/x-www-form-urlencoded" target="_blank">
       ...
    
         <div>
             <button onclick="window.location.pathname = '<%= Url.Action("Edit", "Order", new {id = Model.Order.ID}) %>'">Cancel</button>
             <button onclick="getReport('print')"> Print</button>
         </div>
    </form>
    

    I want to redirect to /Order/Edit/XXX page when I click on Cancel button. Right now it is taking me to /Order/Print page.

    How can I redirect to /Order/Edit/123 page when I click cancel button?