MVC远程验证不起作用,我是MVC的新手,感谢任何帮助:
WebConfig:
<appSettings>
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
控制器:
[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);
}
模特课程:
[Remote("CheckAccount", "Home", ErrorMessage = "Account already exists!")]
public string client_id { get; set; }
查看:
<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>
我不知道为什么它不起作用。 project没有调用CheckAccount()函数。我在视图中添加了脚本,但收到了以下错误消息:
答案 0 :(得分:2)
您只包含部分视图,因此我唯一的建议是确保以下脚本包含在您的视图/布局中:
<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>
答案 1 :(得分:0)
大家都知道:HtmlHelper.UnobtrusiveJavaScriptEnabled设置为false。
更正语法。
@Html.AntiForgeryToken()
HtmlHelper.UnobtrusiveJavaScriptEnabled = true;