我有一個ASP.NET MVC 4,我使用客戶端和服務器端驗證與驗證屬性。使用javascript將驗證錯誤添加到現有表單中?
形式將這樣用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;
}
現在我需要把這些驗證成表格,就好像在客戶端驗證了一樣。我該怎麼做呢?
您是否正在使用模型綁定,並將驗證屬性添加到模型中?您可能不想擁有自己的結果類,就像您使用JEditResult創建的一樣。看起來您正在複製內置於MVC的功能。你能發佈你的模型和你的動作方法的代碼嗎? –
擺脫所有輸入循環業務,並使用[有效()](http://docs.jquery.com/Plugins/Validation/valid)。此外,僅供參考 - 它是「服務器」而不是「服務」 - 就像網絡服務器一樣。得到它? –
@ jesus.tesh謝謝,這工作正常,但如何處理返回驗證的主要問題尚未解決。 – Ivy