In an ASP.NET MVC razor view I am submitting a form on button click. If I indicate 'return' on onsubmit parameter it behaves different than If I do not indicate 'return'.
So If I parametrize BeginForm as below:
Html.BeginForm("", "", FormMethod.Post, new { id = "myForm", onsubmit = "return doAction();", @class = "well" })
it behaves different from below one:
Html.BeginForm("", "", FormMethod.Post, new { id = "myForm", onsubmit = "doAction();", @class = "well" })
so What is the purpose of using return or not using it?
^ What this would do is just execute your function, and since
doAction()isn't stopping the event from doing the default process, the form will submit.^ What this would do is execute your function and if your function returns a
falseboolean value, it would prevent the form from submitting.To see where they exactly differ, try
onsubmit="doAction()"and a function that returnsfalse;The form will still submit because you didn't indicate the
returnkeyword despite the function returningfalse. Thereturnkeyword will signal the form to check the value returned by the function first.