Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to validate the request using onBegin in Ajax.Begin Form?

Question : This code is not firing the StartValidation function. Why?

<html>
<head>
    <script src="/Scripts/jquery-1.7.1.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $('#frmUp').find("input[type = 'submit']").click(function () {
                $('#frmUp').submit();
            });
        });
        function StartValidation() {
            return true;
        }
    </script>
</head>
<body>
    <div>
        @using (Ajax.BeginForm("UploadRequestFile", "FileUpload", new AjaxOptions
            {
                HttpMethod = "POST",
                OnBegin = "return StartValidation();"
            }, new { id = "frmUp" }))
        {
            <input type="submit" name="Submit" value="Submit" />
        }
    </div>
</body>
</html>

Runtime markUp

<form method="post" id="frmUp" data-ajax-method="POST" 
      data-ajax-begin="return StartValidation();" data-ajax="true" 
      action="/fileupload/UploadRequestFile?Length=10">            
      <input type="submit" value="Submit" name="Submit">
</form>
like image 960
Imad Alazani Avatar asked Dec 16 '22 09:12

Imad Alazani


1 Answers

This code is not firing the StartValidation function. Why?

Because you seem to have subscribed to the click event of the submit button, which you absolutely don't need.

All you need is to reference the jquery.unobtrsuive-ajax.js script in order to AJAXify those Ajax.* helpers:

<html>
<head>
    <script src="/Scripts/jquery-1.7.1.min.js" type="text/javascript"></script>
    <script src="/Scripts/jquer.unobtrusive-ajax.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        function StartValidation() {
            return true;
        }
    </script>
</head>
<body>
    <div>
        @using (Ajax.BeginForm("UploadRequestFile", "FileUpload", new AjaxOptions
            {
                HttpMethod = "POST",
                OnBegin = "return StartValidation();"
            }, new { id = "frmUp" }))
        {
            <input type="submit" name="Submit" value="Submit" />
        }
    </div>
</body>
</html>

You are undoubtefully noticing how I added a reference to the jquery.unobtrusive-ajax.min.js script in order to make those Ajax.* helpers do what you expect them to do.

Another remark I have about your code is that you seem to be attempting to invoke an action named UploadRequestFile on the FileUpload controller. Given those names I can safely assume that you are attempting to upload a file to the server (it's just that you didn't show or mention that in your question). Let me tell you quickly, before you get upset, that you cannot upload files using AJAX. You could use some plugin such as Uploadify or BlueImp File Upload or if your client browsers support the HTML5 File API then you could rely on it.

like image 153
Darin Dimitrov Avatar answered Mar 06 '23 00:03

Darin Dimitrov