razor - Ajax.BeginForm 和 OnBegin 防止 Action 被调用

标签 razor ajax.beginform

我在我的 MVC 3 + Razor 应用程序中使用 Ajax.Begin 表单

    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;

    }

最佳答案

找到了!

只需将我的 OnBegin 属性调整为

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

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

关于razor - Ajax.BeginForm 和 OnBegin 防止 Action 被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10024135/

相关文章:

asp.net-mvc-3 - 为什么CreateView中的masterPath参数为空?

c# - ASP.NET MVC 局部 View : How to fix "Error executing child request..." error?

c# - RenderSection() inside partial with master page

ajax - Ajax.BeginForm 的 $.ajaxSetup

javascript - 如何使用asp.net mvc解决Ajax.BeginForm调用两次

asp.net-mvc-3 - MVC3 - Razor,Ajax.BeginForm - 客户端 - OnBegin

javascript - 是否可以将事件传递给 Ajax 表单的 OnBegin 函数?

asp.net-mvc-4 - MVC Razor 强类型 View 上表行的回发复选框

asp.net-mvc - 如何从Ajax表单中在asp.net mvc中提交下拉列表

c# - 数据库中的 Asp.net MVC 页面标题