远程验证方法不会触发

时间:2018-02-01 13:16:38

标签: c# asp.net-mvc remote-validation

我正在使用远程验证。我想验证天气电话号码是否存在。但远程方法没有调用。我已经看到他们正在添加jquery validate cdn以进行远程验证的一些解决方案。我已经把这些cdn但仍存在问题。 这是代码。

<head>
    <meta name="viewport" content="width=device-width" />
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <link rel="" href="https://cdnjs.cloudflare.com/ajax/libs/validate-js/2.0.1/validate.min.js" />
    <link rel="" href="https://cdnjs.cloudflare.com/ajax/libs/jquery-validation-unobtrusive/3.2.6/jquery.validate.unobtrusive.min.js" />
    <title>Index</title>
</head>
<form name="frmCreateList" method="post" action="@Url.Action("Index","Home")">
<div class="form-group">
                                <div class="row">
                                    <div class="col-md-4">
                                        <span><b>Employee Phone</b></span>
                                    </div>
                                    <div class="col-md-8">
                                        @Html.TextBoxFor(x => x.EmpPhone, new { @class = "form-control" })
                                        @Html.ValidationMessageFor(x => x.EmpPhone,"", new { @class="text-danger" })
                                    </div>
                                </div>
                            </div>
                            </form>
                            
                             <button type="submit" class="btn btn-primary">Submit</button>
                             
                             
Here is model.

[Required(ErrorMessage ="Please Enter Phone")]
        [Remote("CheckPhoneNumber","Validation","Phone number already exists")]
        public string EmpPhone { get; set; }
        
        
        
Here is Remote Method in controller.

 [AllowAnonymous]
        public JsonResult CheckPhoneNumber(string EmpPhone)
        {
            var record = _listEmp.SingleOrDefault(x => x.EmpPhone == EmpPhone);
            if(record != null)
            {
                return Json(false, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(true, JsonRequestBehavior.AllowGet);
            }
        }

1 个答案:

答案 0 :(得分:2)

您的模型和视图绑定是最新的,但脚本渲染存在问题 您将外部脚本作为link标记中的样式表进行处理,它应该在<script>中 标签 更改脚本呈现

<script src="https://cdnjs.cloudflare.com/ajax/libs/validate-js/2.0.1/validate.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validation-unobtrusive/3.2.6/jquery.validate.unobtrusive.min.js"></script>

和提交按钮应位于<form></form>

<form name="frmCreateList" method="post" action="@Url.Action("Index","Home")">
    <div class="form-group">
        <div class="row">
            <div class="col-md-4">
                <span><b>Employee Phone</b></span>
            </div>
            <div class="col-md-8">
                @Html.TextBoxFor(x => x.EmpPhone, new { @class = "form-control" })
                @Html.ValidationMessageFor(x => x.EmpPhone, "", new { @class = "text-danger" })
            </div>
        </div>
    </div>

    <button type="submit" class="btn btn-primary">Submit</button>
</form>

我希望它能帮助你。谢谢