ASP.NET MVC 4があり、検証属性を使用してクライアント側とサーバー側の検証を使用しています。
フォームは、次のようにajaxで送信する前に適切に検証されます。
function ValidateFormAndAjaxSubmit(formId, callingElement) {
if (IsNotDblClick(callingElement.id)) {
var _form = $("#" + formId);
var validator = _form.validate();
var anyError = false;
_form.find("input").each(function () {
if (!validator.element(this)) { // validate every input element inside this step
anyError = true;
}
});
if (anyError) {
window.latestClick = '';
return false; // exit if any error found
}
$.post(_form.attr("action"), _form.serialize(), function (data) {
if (data.Success) {
}
else {
}
alert("Win");
window.latestClick = '';
//check the result and do whatever you want
})
}
}
フォームは次のようになります:
@using (Html.BeginForm("JEdit", "Post", FormMethod.Post, new { id = "frmEditPost" }))
{
@Html.ValidationSummary(true)
...
}
サーバーサイドで追加の検証が行われ、これは次のような戻りクラスに格納されます。
public class JEditResult
{
public Boolean Success = false;
public string TitleErrMessage;
public string TextErrMessage;
public string LinkErrMessage;
public string TagErrMessage;
public string OtherErrMessage;
}
次に、これらの検証を、クライアントで検証されたかのようにフォームに入力する必要があります。どうすればよいですか?