asp.net mvc - Prevent MVC form submit using Javascript Function -
i have upload.ascx
in project. loaded inside jquery popup.
the upload.ascx
contains file upload
control. file upload control uploads .xlsx , .xls
files. have added java script
function validation (to prevent unnecessary files uploaded).
onchange of fileupload
control , onclick of submit button
same validation function called.
the file upload validation function working both calling. if user clicks submit button same function getting called , working fine.
my problem is: in validation function have written return false
. after displaying alert message, page getting redirected url (localhost/admin/authorization/acceptfile.aspx
). acceptfile
actionresult name. should not happen. function returning false. form getting redirected above url why.?
i have kept debugger in action result not getting called if wrong file(its correct). if correct file index file loaded success message(action result getting called , working fine). .
i suspect mvc form. if wrong file uploaded redirection happening out calling action result should stopped.
<% using (html.beginform("acceptfile", "authorization", formmethod.post, new { enctype = "multipart/form-data" })) {%>
my javascript function:
function checkfile(sender) { var validexts = new array(".xlsx", ".xls"); var fileext = sender.value; var strvalue = false; fileext = fileext.substring(fileext.lastindexof('.')); if (validexts.indexof(fileext) < 0) { showmessage(-1, "invalid file selected, valid files .xlsx , .xls types."); strvalue = false; } else { strvalue = true; } return strvalue; }
my upload.ascx code:
<div> <% using (html.beginform("acceptfile", "authorization", formmethod.post, new { enctype = "multipart/form-data" })) {%> <input type="file" id="fileauthorization" name="fileauthorization" onchange="checkfile(this);" /> <input type="submit" id="btnsave" name="btnsave" value="upload" onclick="javascript:return checkfile(document.getelementbyid('fileauthorization'))" /> <%} %> </div>
my self found problem.
yes suspect correct. problem is
onsubmit = "javascript:return checkfile(document.getelementbyid('fileauthorization'))"
this should called in form tag itself:
<% using (html.beginform("acceptfile", "authorization", formmethod.post, new { enctype = "multipart/form-data", onsubmit = "javascript:return checkfile(document.getelementbyid('fileauthorization'))" })) {%>
but earlier called submit button onclick
<input type="submit" id="btnsave" name="btnsave" value="upload" onclick="javascript:return checkfile(document.getelementbyid('fileauthorization'))" />
i have corrected , getting worked(validations , file upload submit button onclick , file upload control onchange).
but question why return false
not working on submit button click? working fileupload onchange events.
why redirection happened.? after changing same line mvc form getting worked (redirection not happening now) why.?
Comments
Post a Comment