远程验证无效

时间:2015-05-06 09:21:19

标签: c# jquery json asp.net-mvc asp.net-mvc-4

当我在文本框中输入电子邮件的值时,它没有触发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: { 并且它正在工作。

有什么想法吗?为什么呢?

1 个答案:

答案 0 :(得分:2)

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

[Remote("VerifyUserEmail", "User"]

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

除了真正的返回之外,其他任何东西都是发送回客户端的错误消息。如果你添加消息而不是返回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" />
}

屏幕截图

screen shot 1

screen shot 2