繁体   English   中英

远程验证无效

[英]Remote validation is not working

当我在文本框中输入电子邮件的值时,它没有触发VerifyUserEmail方法。 以下是我的代码。 我的错误在哪里?

视图

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

模型

[DisplayName("Email")]       
[Remote("VerifyUserEmail", "User", ErrorMessage = "Email already exists!")]
public string User_Email { get; set; }

调节器

    public JsonResult VerifyUserEmail(string User_Email)
    {
        try
        {
            using (RKDYMEntities objConnection = new RKDYMEntities())
            {
                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(false, JsonRequestBehavior.AllowGet);
                }
                else
                {
                    return Json(true, JsonRequestBehavior.AllowGet);
                }


            }
        }
        catch (Exception Ex)
        {
            return Json(false, JsonRequestBehavior.AllowGet);
        }
    }

我还在web.config中添加了<add key="ClientValidationEnabled" value="true" /> <add key="UnobtrusiveJavaScriptEnabled" value="true" />

下面是生成的html代码:

 <div class="form-group">
    <label class="control-label col-lg-2" for="User_Email">Email</label>
        <div class="col-lg-4">
            <input class="text-box single-line" data-val="true" data-val-length="The field Email must be a string with a maximum length of 200." data-val-length-max="200" data-val-remote="Email already exists!" data-val-remote-additionalfields="*.User_Email" data-val-remote-url="/User/VerifyUserEmail" id="User_Email" name="User_Email" type="text" value="" />
            <span class="field-validation-valid" data-valmsg-for="User_Email" data-valmsg-replace="true"></span>
       </div>
 </div>

Jquery验证

我正在使用客户端JQuery验证工作正常。

$(document).ready(function () {
    $("#Registration").validate({
        rules: {
            User_Email: {
                required: true, email: true
            },
            User_Password: {
                required: true,
                minlength: 8
            },
            User_ReTypePassword: {
                required: true,
                minlength: 8,
                equalTo: '#User_Password'
            },
            User_FirstName: { required: true },
            User_LastName: { required: true },
            User_ZipCode: {
                required: true,
                digits: true
            }
        },
        messages: {
            User_Email: {
                required: 'Email is required', email: 'Invalid email'
            },
            User_Password: {
                required: 'Password is required',
                minlength: 'Min length of password is 8'
            },
            User_ReTypePassword:
                {
                    required: 'Confirm Password is required',
                    equalTo: 'Confirm Password must be equal to password'
                },
            User_ZipCode: { required: 'ZipCode is required', digits: 'Only digits are allowed' },
            User_FirstName: { required: 'First Name is required' },
            User_LastName: { required: 'Last Name is required' }
        },
        errorPlacement: function (error, element) {
            error.insertAfter(element);
        },
        showErrors: function (errorMap, errorList) {
            this.defaultShowErrors();
        }
    });
});

编辑

我已经删除了

$("#Registration").validate({ // rules: {并且它正在运行。

任何的想法? 为什么?

从不使用data属性中的错误消息(您可以使用下面的简单示例来测试它):

[Remote("VerifyUserEmail", "User"]

远程接受带有错误消息的重载但不对其执行任何操作,您应该从服务器端返回错误消息。

基本上除了真正的返回之外的任何东西都是发送回客户端的错误消息。 如果您添加消息而不是返回false,您应该看到这一点。

 return Json(string.Format("{0} does not exist.", User_Email),
                JsonRequestBehavior.AllowGet); 

您还需要确保您拥有以下参考:

<script src="http://ajax.microsoft.com/ajax/jQuery/jquery-1.4.2.min.js"></script>
<script src="http://ajax.microsoft.com/ajax/jquery.validate/1.7/jquery.validate.min.js"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

简单的工作示例

模型

public class AModel
{
    [DisplayName("Email")]
    [Remote("VerifyUserEmail", "Home")]
    public string User_Email { get; set; }
}

控制器:

    public JsonResult VerifyUserEmail(string User_Email)
    {

            if (User_Email == "1")
            {
                return Json("No", JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(true, JsonRequestBehavior.AllowGet);
            }
    }

视图

<script src="http://ajax.microsoft.com/ajax/jQuery/jquery-1.4.2.min.js"></script>
<script src="http://ajax.microsoft.com/ajax/jquery.validate/1.7/jquery.validate.min.js"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Html.BeginForm())
{

    <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>
    <input type="submit" />
}

屏幕截图

屏幕截图1

屏幕截图2

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM