本文介绍了具有OnBegin的Ajax.BeginForm阻止调用动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MVC 3 + Razor应用程序中使用Ajax.Begin Form

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" />
   }

下面是我的onBegin方法的外观。我向该方法传递了一个值,便能够得到适当的警报。

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.

请对此提供帮助。

下面是我实际的验证方法

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;

    }


推荐答案

找到了它!

只需将我的OnBegin属性调整为

just had to tweak my OnBegin property to

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

我引用的链接

这篇关于具有OnBegin的Ajax.BeginForm阻止调用动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-13 22:43