Ajax.BeginForm with OnBegin prevent action to be called

一曲冷凌霜 提交于 2019-11-30 23:30:36

问题


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;

    }

回答1:


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?



来源:https://stackoverflow.com/questions/10024135/ajax-beginform-with-onbegin-prevent-action-to-be-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!