简体   繁体   English

防止使用Javascript功能提交MVC表单

[英]Prevent MVC form submit using Javascript Function

I have a upload.ascx in my project. 我的项目中有一个upload.ascx It will be loaded inside Jquery Popup. 它将被加载到Jquery Popup内部。

The upload.ascx contains File Upload control. upload.ascx包含File Upload控件。 The File upload control will uploads .xlsx and .xls files. 文件上载控件将上载.xlsx and .xls文件。 I have added Java script function to do this validation (To prevent unnecessary files uploaded). 我添加了Java script功能来进行此验证(以防止上传不必要的文件)。

Onchange of FileUpload control and Onclick of Submit button the same Validation function will be called. Onchange of FileUpload控件的Onchange of FileUploadOnclick of Submit buttonOnclick of Submit button将调用相同的Validation函数。

The File Upload Validation Function is working for both calling. 文件上传验证功能可同时用于两个调用。 If the user clicks submit button the same function is getting called and working fine. 如果用户单击提交按钮,则将调用相同的功能,并且可以正常工作。

My Problem is: In My validation function i have written return false . 我的问题是:在我的验证函数中,我编写了return false After displaying the alert Message, the Page is getting redirected to some URL ( localhost/Admin/Authorization/AcceptFile.aspx ). 显示警报消息后,页面将被重定向到某个URL( localhost/Admin/Authorization/AcceptFile.aspx )。 AcceptFile is my ActionResult name. AcceptFile是我的ActionResult名称。 It should not happen. 它不应该发生。 The Function is returning False. 该函数返回False。 But the Form is getting redirected to Above URL why.? 但是为什么表单被重定向到上方URL。

I have kept debugger in my Action Result that is not getting called if its wrong File(Its correct). 我将调试器保留在我的操作结果中,如果错误的File(正确),调试器不会被调用。 If its correct File the Index file will be loaded with Success message(Action result is getting called and working Fine). 如果其文件正确,索引文件将加载成功消息(正在调用操作结果并可以正常工作)。 .

I suspect MVC Form. 我怀疑是MVC表格。 If wrong file is uploaded the redirection is happening with out calling Action Result this should be stopped. 如果上传了错误的文件,则重定向发生而没有调用操作结果,因此应停止该操作。

<% using (Html.BeginForm("AcceptFile", "Authorization", FormMethod.Post, new { enctype = "multipart/form-data" }))
       {%>

My Javascript Function: 我的Javascript函数:

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 are .xlsx and .xls types.");
            strValue = false;
        }
        else {
            strValue = true;
        }
        return strValue;
    }

My upload.ascx Code: 我的upload.ascx代码:

<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 i found the Problem. 我自己发现了问题。

Yes My suspect is correct. 是的,我的怀疑者是正确的。 The Problem is 问题是

onsubmit = "javascript:return checkfile(document.getElementById('fileAuthorization'))"

this should be called Like this in Form tag itself: 在Form标记本身中应将其命名为:

 <% using (Html.BeginForm("AcceptFile", "Authorization", FormMethod.Post, new { enctype = "multipart/form-data", onsubmit = "javascript:return checkfile(document.getElementById('fileAuthorization'))" }))
       {%>

But Earlier it was called Submit button Onclick 但在早期它被称为“提交”按钮Onclick

<input type="submit" id="btnSave" name="btnSave" value="Upload" onclick="javascript:return checkfile(document.getElementById('fileAuthorization'))" />

I have corrected and its getting worked(Validations and File Upload For Submit button Onclick and File Upload control onchange). 我已经更正并且开始工作(“提交”按钮的“验证和文件上传”按钮Onclick和“文件上传”控件onchange)。

But My question is why the return false is not working on submit button click? 但是我的问题是为什么return false在提交按钮单击上不起作用? But its working for Fileupload onchange events. 但是它适用于Fileupload onchange事件。

why the redirection was happened.? 为什么发生重定向。? after changing the same Line to MVC Form its getting worked (redirection is not happening now) why.? 在将同一行更改为MVC表单后,其正常工作(现在不发生重定向)为什么?

Change your submit to this: 将您的提交更改为此:

<button type="button" id="btnSave" name="btnSave" onclick="checkfile(document.getElementById('fileAuthorization'))" ></button>

And it won't submit the form automatically 而且它不会自动提交表单

When you're ready to submit the form you can do so with javascript in your callback function: 当您准备好提交表单时,可以在回调函数中使用javascript进行提交:

document.getElementById('form-id').submit();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM