Yas*_*ser 10 ajax.beginform razor
我在我的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" />
}
Run Code Online (Sandbox Code Playgroud)
下面是我的onBegin方法的样子.我传递一个值给这个方法,我能够得到一个适当的警报.
function ValidateDateFunction(id) {
alert(id);
if(some-ConditionUsing-formId)
{
return false;
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
现在使用这个我希望如果我的条件失败,那么不应该调用动作.但在我的情况下,在这两种情况下都会调用我的动作.
请帮忙.
以下是我的实际验证方法
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;
}
Run Code Online (Sandbox Code Playgroud)
Yas*_*ser 17
找到了 !
只需要将我的OnBegin属性调整为
OnBegin = "return ValidateDateFunction('" + @abc.xyz + "')"
Run Code Online (Sandbox Code Playgroud)
链接我提到 ASP.Net MVC 3.0 Ajax.ActionLink Onbegin函数是真的执行动作?