0

i am using remote validation. i want to validate weather the phone no is exist or not. But Remote method does not called. I have seen some of solution they were adding jquery validate cdn for remote validation. I have put these cdn but still problem exists. here is code.

<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);
            }
        }
shami sheikh
  • 552
  • 3
  • 13
  • 26
  • Any js errors in browser console? Also check [article](https://msdn.microsoft.com/en-us/library/gg508808(VS.98).aspx) about remove validation for appSettings configs. – aleha_84 Feb 01 '18 at 13:32
  • Thanks for your concern but there is no json error in browser console. – shami sheikh Feb 01 '18 at 13:39
  • Then check for network calls to your controller and action in browser dev tools. – aleha_84 Feb 01 '18 at 13:44

1 Answers1

3

your model and view binding is current but there is an issue with script render you are rending external script as stylesheet in link tag it should be in <script> tag change script rendering

<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>

and submit button should be in <form></form>

like

<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>

i hope it should helps you.thanks

Lalji Dhameliya
  • 1,729
  • 1
  • 17
  • 26