[ACCEPTED]-How do I stop a button event from posting in ASP.NET MVC?-asp.net-mvc
Set the type
to button
:
This will prevent the Form from Submitting.
No 8 additional javascript functions necessary.
Note: When 7 this property is not set, it will default to submit
.
I 6 assume your button
Controls are inside a form
tag 5 or Html.BeginForm()
code block.
Below are some Examples:
<button onclick="alert('You Clicked Implicit Submit.')">Implicit Submit</button>//Post Back Form.
<button onclick="alert('You Clicked Submit.')" type="submit">Submit</button>//Post Back Form.
<button onclick="alert('You Clicked Button.')" type="button">Button</button>//Stay on Client Page.
For 4 a simple redirect (without Posting the Form 3 back to its Default Action) you could also 2 do this:
<button onclick="location.href='@Url.Action("Action", "Controller")'" type="button">Redirect</button>
Special thanks to the Answer found 1 here: https://stackoverflow.com/a/17452739/555798
In your event handler you need to do:
$("button").click(function(event){
event.preventDefault();
// do something
});
taken 1 from: http://docs.jquery.com/Events/jQuery.Event#event.preventDefault.28.29
update This should work in your code:
$('#submit').bind('click', submit_click);
function submit_click(event) {
event.preventDefault();
alert('clicked submit');
}
Add this to your click event bindings:
$('#submit').bind('click', submit_click);
function submit_click() {
alert('clicked submit');
return false;
}
Returning 3 false stops the regular behaviour of the 2 button happening, instead only performing 1 your code.
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.