我在我的 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/