带OnBegin的Ajax.BeginForm会阻止调用操作

时间:2012-04-05 07:14:04

标签: razor ajax.beginform

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

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

以下是我的onBegin方法的样子。我正在将值传递给此方法,我能够获得适当的警报。

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

        return true;           
    }

现在使用这个我希望如果我的条件失败,那么不应该调用动作。但在我的情况下,在这两种情况下都会调用我的动作。

请帮忙。

以下是我的实际验证方法

        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 个答案:

答案 0 :(得分:17)

找到它了!

只需要将我的OnBegin属性调整为

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

我提到的链接 ASP.Net MVC 3.0 Ajax.ActionLink Onbegin Function true the execute the action?

相关问题