Search code examples
c#asp.net-mvcremote-validationvalidationattribute

MVC Remote Validation Not calling function or working


MVC Remote Validation not working, I'm new to MVC, any help is appreciated:

WebConfig:

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

Controller:

   [AllowAnonymous]
    public JsonResult CheckAccount(string client_id)
    {
        var user = db.LGC_InboundData_c.Where(x => x.client_id == 
         client_id.Trim());
        return !user.Any() ?
           Json(true, JsonRequestBehavior.AllowGet) :
           Json(string.Format("{0} is already exists."),
               JsonRequestBehavior.AllowGet);
    }

Model Class:

[Remote("CheckAccount", "Home", ErrorMessage = "Account already exists!")]
        public string client_id { get; set; }

View:

    <div class="col-xs-3">
        @Html.LabelFor(model => model.client_id, htmlAttributes: new { @class = "col-xs-10" })
        <div class="col-md-12">
            @Html.EditorFor(model => model.client_id, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.client_id, "", new { @class = "text-danger" })
        </div>
    </div>

I have no idea why its not working. project is not calling the CheckAccount() function. I added the scripts in my view, but got this error message:

enter image description here


Solution

  • Just so everyone knows: HtmlHelper.UnobtrusiveJavaScriptEnabled was set to false.

    Correct Syntax.

    @Html.AntiForgeryToken()
        HtmlHelper.UnobtrusiveJavaScriptEnabled = true;