MVC远程验证(唯一的电子邮件地址) [英] MVC Remote validation (unique email address)

查看:138
本文介绍了MVC远程验证(唯一的电子邮件地址)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在那里的用户输入他们的电子邮件地址的注册页面。
我想每封电子邮件是独一无二的。

I'm currently working on a registration page where the users enter their email address. I want every email to be unique.

这是我的RegisterModel的一部分:

This is a part of my RegisterModel:

    [Required()]
    [System.Web.Mvc.Remote("IsUserEmailAvailable", "Account")]
    [EmailAddress()]
    [DataType(DataType.EmailAddress)]
    [Display(Name = "Email")]
    public string Email { get; set; }

..

这是我的AccountController的一部分:

This is a part of my accountcontroller:

    public JsonResult IsUserEmailAvailable(string UserEmail)
    {
        return Json(!db.UserProfiles.Any(User => User.UserName == UserEmail),       JsonRequestBehavior.AllowGet);
    }

这是我的我的看法:

This my my view:

<script src="~/Scripts/jquery-1.7.1.min.js" type="text/javascript"></script>
<script src="~/Scripts/jquery.validate.min.js" type="text/javascript"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js" type="text/javascript"></script>


<hgroup class="title">
    <h1>Create Account</h1>
</hgroup>

@using (Html.BeginForm()) {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary()

    <fieldset>
        <legend>Registration Form</legend>
        <ol>
            <li>
                @Html.LabelFor(m => m.UserName)
                @Html.TextBoxFor(m => m.UserName)
            </li>
            <li>
                @Html.LabelFor(m => m.Password)
                @Html.PasswordFor(m => m.Password)
            </li>
            <li>
                @Html.LabelFor(m => m.ConfirmPassword)
                @Html.PasswordFor(m => m.ConfirmPassword)
            </li>
            <li>
                @Html.LabelFor(m => m.Email)
                @Html.TextBoxFor(m => m.Email)
            </li>
            <li>
                @Html.LabelFor(m => m.ConfirmEmail)
                @Html.TextBoxFor(m => m.ConfirmEmail)
            </li>
        </ol>
        <input type="submit" value="Registrera" />
    </fieldset>
}

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

如果我删除[System.Web.Mvc.Remote(IsUserEmailAvailable,账户)一切都很好,除了EMAILADDRESS不会是唯一的。有了它,当我preSS提交,没有任何反应。

If I remove the [System.Web.Mvc.Remote("IsUserEmailAvailable", "Account")] everything is fine except the emailaddress wont be unique. With it, when I press the Submit, nothing happens.

我错过了什么?

推荐答案

@Daniel J.G.是正确的。添加[使用AllowAnonymous]的功能使得访问。

@Daniel J.G. is correct. Adding [AllowAnonymous] to the function makes it accessible.

这篇关于MVC远程验证(唯一的电子邮件地址)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆