Ajax.BeginForm with OnBegin prevent action to be called
Asked Answered
H

1

13

I am using Ajax.Begin Form in my MVC 3 + Razor application

    using (Ajax.BeginForm("ActionName", "ControllerName", new AjaxOptions { OnBegin = "ValidateDateFunction('" + @abc.xyz + "')", HttpMethod = "POST", UpdateTargetId = "savebutton" }))
   {
         <input type="submit" value="Save" />
   }

Below is how my onBegin method looks like. I am passing a value to this method, I am able to get a proper alert.

    function ValidateDateFunction(id) {
        alert(id);
        if(some-ConditionUsing-formId)
        {
            return false;
        }

        return true;           
    }

Now using this I wanted that if my condition fails then action should not be called. But here in my case in both condition my action is called.

Please help on this.

Below is my actual validate method

        function ValidateDateFunction(fId) {

        var first = document.getElementById("startDate" + fId);
        var second = document.getElementById("endDate" + fId);

        if (first.value == "" && second.value != "") {
            alert("Please select both dates");
            return false;
        }
        else if (first.value != "" && second.value == "") {
            alert("Please select both dates");
            return false;
        }

        var startDateVal = new Date(first.value);
        var endDateVal = new Date(second.value);

        if (startDateVal.getTime() > endDateVal.getTime()) {
            alert("Error ! The start date is after the end date!");
            return false;
        }
        alert('should not reach here');
        return true;

    }
Haematocryal answered 5/4, 2012 at 7:14 Comment(3)
found it ! just had to tweak my OnBegin property to OnBegin = "return ValidateDateFunction('" + @abc.xyz + "')" Link I referred #8057468Haematocryal
what do you want to pass to it?Clemmie
found it #10024635Haematocryal
H
18

found it !

just had to tweak my OnBegin property to

OnBegin = "return ValidateDateFunction('" + @abc.xyz + "')"

Links I referred ASP.Net MVC 3.0 Ajax.ActionLink Onbegin Function true the execute the action?

Haematocryal answered 6/4, 2012 at 5:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.