MVC远程验证未呈现

时间:2017-02-02 08:29:42

标签: jquery asp.net-mvc remote-validation

我正在为我的模型使用类库并在Area中创建我的控制器 现在我想使用远程验证,但它没有工作,实际上它没有渲染。

我在模型类库中的模型

[System.ComponentModel.DataAnnotations.RegularExpression(@"(?=.*[a-zA-Z0-9]).{4,20}$", ErrorMessage = "error")]
[System.ComponentModel.DataAnnotations.Required(ErrorMessage = "error")]
[System.Web.Mvc.Remote("UserNameExist","AdminUser","Admin",ErrorMessage="error")]
public string UserName { get; set; }

管理员区域中的控制器

[HttpPost]
public JsonResult UserNameExist(string UserName)
{
    using (DataBaseContext dbContext = new DataBaseContext())
    {
        var user = dbContext.Users.SingleOrDefault(current => current.UserName == UserName);
        return Json(user == null);
    }
}

我还添加了jquery和jquery.validation以及jquery.validate.unobtrusive 我把它添加到我的webconfig

<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />

更新

渲染的html是:

<input class="form-control directionEn valid" data-val="true" data-val-regex="error" data-val-regex-pattern="(?=.*[a-zA-Z0-9]).{4,20}$" data-val-required="error" id="UserName" name="UserName" placeholder="User Name" type="text" value="">

没有远程验证属性

用户视图:

@model Models.User
@section Scripts
{
    <script src="~/Scripts/jquery.validate.min.js"></script>
    <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
}
<div class="row">
    <div class="col-lg-6 col-lg-offset-3 col-md-6 col-md-offset-3">
        <div class="form-group">
            <a class="btn btn-success" href="/Admin/AdminUser/List">List</a>
        </div>
    </div>
</div>
<div class="row">
    <div class="col-lg-6 col-lg-offset-3 col-md-6 col-md-offset-3">
        <div class="panel panel-default">
            <div class="panel-heading">
                <h4>Add New User</h4>
            </div>
            <div class="panel-body">
                @using (Html.BeginForm("Add", "AdminUser", null, FormMethod.Post, new { enctype = "multipart/form-data" }))
                {
                    @Html.Raw(System.Web.HttpUtility.HtmlDecode((Html.ValidationSummary(true, "", htmlAttributes: new { @class = "alert alert-danger" }) ?? (object)"").ToString()))

                    <div class="form-group">
                        <label>
                            UserName
                        </label>
                        @Html.TextBoxFor(model => model.UserName, htmlAttributes: new { @class = "form-control directionEn", @placeholder = "User Name" })
                        @Html.ValidationMessageFor(model => model.UserName, "", htmlAttributes: new { @class = "validationError" })
                    </div>

                }
            </div>
        </div>
    </div>
</div>

我的代码中出了什么问题???

1 个答案:

答案 0 :(得分:0)

首先从Controller中删除HttpPost。我不认为RemoteValidation可以在HttpPost上运行

public JsonResult UserNameExist(string UserName)
{
    using (DataBaseContext dbContext = new DataBaseContext())
    {
        var user = dbContext.Users.SingleOrDefault(current => current.UserName == UserName);
        return Json(user == null);
    }
}

其次在您的模型中,我认为没有使用管理员参数

[System.ComponentModel.DataAnnotations.RegularExpression(@"(?=.*[a-zA-Z0-9]).{4,20}$", ErrorMessage = "error")]
[System.ComponentModel.DataAnnotations.Required(ErrorMessage = "error")]
[System.Web.Mvc.Remote("UserNameExist","AdminUser",,ErrorMessage="error")]
public string UserName { get; set; }

此外,我认为您有显示验证消息

@Html.TextBoxFor(i => i.UserName)
@Html.ValidationMessageFor(i => i.UserName)

您还可以将debbuger放在UserNameExist函数上并检查该函数是否被调用。

我的工作小提琴手:https://dotnetfiddle.net/dlME8p