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;
}