0

I am trying to return to a different page on button click but i'm unable to achieve . It hits the controller action method(onclick) but the view remains the same & url in the browser remains unchanged .

Html:

 <input type="submit" class="btn btn-primary" value="Login" onclick="window.location.href='@Url.Action("Home", "Index")' "/>

Even when i keep http://www.google.com it doesnt seems to work .

am i missing anything important here

super cool
  • 6,003
  • 2
  • 30
  • 63
  • 2
    Try using `input type="button"...` instead. – DavidG Apr 20 '15 at 12:45
  • holy jesus *it worked* . i tried – super cool Apr 20 '15 at 12:49

1 Answers1

1

An input of type submit will try to submit a form. The JavaScript you have attached to that button will run but then immediately get "overridden" by the button trying to do something else. You can do a couple of things:

Option 1: Use a different type such as button:

<input type="button" onclick="window.location.href='@Url.Action("Home", "Index")' "/>

Option 2: Return false in your JavaScript, this prevents the default action from happening.

<input type="submit" class="btn btn-primary" value="Login" 
    onclick="window.location.href='@Url.Action("Home", "Index"); return false;' "/>

A nice description of preventing the default action from happening is here.

Community
  • 1
  • 1
DavidG
  • 113,891
  • 12
  • 217
  • 223