我有一个 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;
}
现在我需要将这些验证放入表单中,就像在客户端上验证了一样。我该怎么做呢?