简体   繁体   中英

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:

在此处输入图片说明

You only included a portion of your View, so my only recommendation can be to make sure that the following scripts are including in your View/Layout:

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

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

Correct Syntax.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM