验证在ASP.NET MVC4中不起作用的唯一用户名

时间:2013-11-21 19:56:22

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

我尝试验证用户名的唯一性,但由于某些原因,它无法正常工作。我是按照以下方式做的

MODEL

public class System_User
    {
        public int Id { get; set; }

        [Required]
        public string FirstName { get; set; }

        [Required]
        public string LastName { get; set; }

        [Required]
        [Remote("doesUserNameExist", "System_User", HttpMethod = "POST", ErrorMessage = "User name already exists. Please enter a different user name.")]
        public string UserName { get; set; }

        [Required]
        [DataType(DataType.Password)]
        public string Password { get; set; }
        public bool IsAdmin { get; set; }
    }

控制器

我已将以下代码添加到System_User控制器

 [HttpPost]
        public JsonResult doesUserNameExist(string UserName)
        {

            var user = Membership.GetUser(UserName);

            return Json(user == null);
        }

查看 我有mvc生成的默认创建视图,其中包含对jquery的必要引用

@model WhiteBoard.Models.System_User

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>

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


    <table class="userTable">
        <tr>
            <td>
                @Html.LabelFor(model => model.FirstName)
            </td>

            <td>
                @Html.EditorFor(model => model.FirstName)
                @Html.ValidationMessageFor(model => model.FirstName)
            </td>
        </tr>
        <tr>
            <td>
                @Html.LabelFor(model => model.LastName)
            </td>

            <td>
                @Html.EditorFor(model => model.LastName)
                @Html.ValidationMessageFor(model => model.LastName)
            </td>
        </tr>
        <tr>
            <td>
                @Html.LabelFor(model => model.UserName)
            </td>

            <td>
                @Html.EditorFor(model => model.UserName)
                @Html.ValidationMessageFor(model => model.UserName)
            </td>
        </tr>
        <tr>
            <td>
                @Html.LabelFor(model => model.Password)
            </td>

            <td>
                @Html.EditorFor(model => model.Password)
                @Html.ValidationMessageFor(model => model.Password)
            </td>
        </tr>


        <tr>
            <td>
                @Html.LabelFor(model => model.IsAdmin)
            </td>

            <td>
                @Html.EditorFor(model => model.IsAdmin)
                @Html.ValidationMessageFor(model => model.IsAdmin)
            </td>
        </tr>
        <tr>

            <td colspan="2">
                <input class="btn btn-success" type="submit" value="Create User" />
            </td>
        </tr>
    </table>
}
<div>
    <p>

        @Html.ActionLink("Back to List", "Index")
    </p>
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
}

当我运行应用程序时,它既没有抛出异常也没有验证工作。

我可以知道我在哪里犯了错误

1 个答案:

答案 0 :(得分:0)

看起来问题是:

return Json(user == null);

如果用户为null,当您要返回false时,将返回true,用户不存在。将其更改为

return Json(user != null);

如果这不起作用或者==只是一种类型,请在评论中告诉我们您输入的用户名,预期结果是什么,以及实际结果是什么。