我應該顯示從元素後得到json結果的錯誤嗎?即:消息 - 「電子郵件已存在」必須添加到具有錯誤消息css類的元素 - User_Email後面。如何顯示由JSON返回的錯誤元素後
目前,我正在將其顯示在表單頂部。
控制器操作結果
在這裏,我返回JSON。
ObjectParameter objIErrorCode = new ObjectParameter("ErrorCode", typeof(Int32));
ObjectParameter objBFlag = new ObjectParameter("bFlg", typeof(bool));
objConnection.Check_User_Exists(User_Email, objBFlag, objIErrorCode);
if (Convert.ToBoolean(objBFlag.Value) != true)
{
return Json(new { Success = false, Message = "Email already exists" }, JsonRequestBehavior.AllowGet);
}
else
{
return Json(new { Success = true, Message = "Email not exists" }, JsonRequestBehavior.AllowGet);
}
jQuery驗證
$("#User_Email").blur(function() {
if ($(this).val() != "") {
$.ajax({
url: "/User/VerifyUserEmail?User_Email=" + $("#User_Email").val(),
success: function (result) {
var success = result.Success;
var message = result.Message;
if (success) {
var errorMsg = message;
$('#msg').html(errorMsg);
$('#msg').show();
}
else {
var errorMsg = null;
$('#msg').html(errorMsg);
$('#msg').hide();
}
}
});
}
return false;
});
查看
<div class="form-group">
@Html.LabelFor(model => model.User_Email, new { @class = "control-label col-lg-2" })
<div class="col-lg-4">
@Html.EditorFor(model => model.User_Email)
@Html.ValidationMessageFor(model => model.User_Email)
</div>
</div>
在您的Jquery代碼中寫入的ID msg控件在哪裏?你爲什麼試圖在你的其他條件中寫入null? –
你有一個'ValidationMessageFor()'屬性,那麼爲什麼你不只是在'User_Email'屬性上使用'[Remote]'屬性,並且不需要你的腳本就可以完成這項工作。 –
@StephenMuecke非常感謝。我正在尋找這樣的解決方案。 –