找到你要的答案

Q:how to display error returned by JSON, after an element

Q:如何显示错误返回的JSON,后一个元素

Should I show error getting from json result after an element? i.e.Message - "Email already exists" must be added after an element - User_Email with error message css class.

Currently I am showing it top of the form.

Controller Action Result

Here I am returning 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 Validation

$("#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;
});

View

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

我应该显示错误从JSON结果后一个元素?即消息”的电子邮件已存在”必须添加元素后- user_email错误讯息的CSS类。

目前我正在展示它的顶部形式。

控制器作用的结果

我在这里返回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>
answer1: 回答1:

I think you div or label named msg is on the top.Place that element below this code.

 @Html.EditorFor(model => model.User_Email);

我认为你的div或标签命名的味精是在顶部的地方,下面这段代码元素。

 @Html.EditorFor(model => model.User_Email);
answer2: 回答2:

In your Jquery validation, you are operating on the html of a tag with id "msg". Whatever function you are calling, e.g. show(), hide(), html(), it is being called on the html object with the id "msg".

$("#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);   // <-------------------- over here
                $('#msg').show(); // <-------------------- over here
            }
            else {
                var errorMsg = null;
                $('#msg').html(errorMsg); // <-------------------- over here
                $('#msg').hide(); // <-------------------- over here
            }
        }
    });
}
return false;
});

Either move the element with the id "msg" to your desired location or what you can do is simply create a new element in your html (for example, a div) AFTER the email address textbox, or wherever on page you want to show the message on, and give it some id, say "EmailMessageContainer".

Modify your code like:

View:

 <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 id="EmailMessageContainer">  //<----- This is the new element, could be a label or a label inside div or whatever you want with whichever class you want
     </div>
 </div>

And Jquery like:

$("#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;                        
                $('#EmailMessageContainer').html(errorMsg);   // <-------------------- over here
                $('#EmailMessageContainer').show(); // <-------------------- over here
            }
            else {
                var errorMsg = null;
                $('#EmailMessageContainer').html(errorMsg); // <-------------------- over here
                $('#EmailMessageContainer').hide(); // <-------------------- over here
            }
        }
    });
}
return false;
});

在你的jQuery验证您所操作的HTML标签ID“味精”。无论要调用的函数,例如show(),hide(),html(),它被称为在HTML对象ID“味精”。

$("#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);   // <-------------------- over here
                $('#msg').show(); // <-------------------- over here
            }
            else {
                var errorMsg = null;
                $('#msg').html(errorMsg); // <-------------------- over here
                $('#msg').hide(); // <-------------------- over here
            }
        }
    });
}
return false;
});

移动元素的ID“味精”到你想要的位置或你能做的只是创建在你的HTML的一个新的元素(例如,一个div)的电子邮件地址的文本框后,或在页面上你想要显示的信息,并给它一些ID,说“emailmessagecontainer”。

修改你的代码:

观:

 <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 id="EmailMessageContainer">  //<----- This is the new element, could be a label or a label inside div or whatever you want with whichever class you want
     </div>
 </div>

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;                        
                $('#EmailMessageContainer').html(errorMsg);   // <-------------------- over here
                $('#EmailMessageContainer').show(); // <-------------------- over here
            }
            else {
                var errorMsg = null;
                $('#EmailMessageContainer').html(errorMsg); // <-------------------- over here
                $('#EmailMessageContainer').hide(); // <-------------------- over here
            }
        }
    });
}
return false;
});
c#  jquery  asp.net-mvc  asp.net-mvc-4