远程验证停止提交按钮保存数据 [英] Remote validation stopping submit button from saving data
问题描述
我在视图上使用远程验证,它可以完美地工作.我遇到的问题是,当我单击提交"按钮时,什么也没有发生,并且焦点返回到了我进行远程验证的字段-表单从未提交.
I'm using remote validation on a view and it works perfectly. The problem I have is that when I click on the submit button nothing happens and focus returns to the field that I have remote validation on - The form never gets submitted.
要设置远程验证,我引用了此页面-https://msdn.microsoft.com/zh-cn/library/gg508808(VS.98).aspx -不知道是否有任何最新的消息.
To set up remote validation I referred to this page - https://msdn.microsoft.com/en-us/library/gg508808(VS.98).aspx - not sure if there's anything more recent available.
如果我从模型中删除 [Remote]
,它将正确提交.我在做什么错了???
If I remove [Remote]
from the model it submits correctly.
What am I doing wrong ???
我的模特是
public class Doctor
{
[Remote("checkEmployeeNumber", "Doctors")]
public string EmployeeNumber { get; set; }
public string DoctorSurname { get; set; }
public string DoctorGivenName { get; set; }
}
控制器
public ActionResult checkEmployeeNumber(string EmployeeNumber)
{
if (EmployeeNumber == null)
{
return Json(false, JsonRequestBehavior.AllowGet);
}
Doctor doctor = db.Doctors.Find(EmployeeNumber);
if (doctor == null)
{
return Json(false, JsonRequestBehavior.AllowGet);
}
return Json(true, JsonRequestBehavior.AllowGet);
}
视图
@model WebApplication1.Models.Doctor
@{
ViewBag.Title = "Create";
}
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
<h2>New Doctor Entry</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.DoctorSurname, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.DoctorSurname, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.DoctorSurname, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.EmployeeNumber, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.EmployeeNumber, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.EmployeeNumber, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.DoctorGivenName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.DoctorGivenName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.DoctorGivenName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
我还将以下内容添加到Web.config
I've also added the following to Web.config
<appSettings>
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
推荐答案
看了2天的问题后,提出问题5分钟后我发现了解决方案!h!
After looking at a problem for 2 days I discover the solution 5 minutes after lodging a question! doh!
似乎我对双重否定词感到困惑.
It seems that I was getting my double negatives confused.
在我的控制器中,如果将它们切换到正确和错误状态,那么它将按预期工作.
In my controller where I've got true and false if you switch them around then it works as expected.
我没想到的一件事是远程验证似乎被激活了两次
One thing I wasn't expecting is that remote validation seems be activated twice
- 离开田野时
- 当您按下表单上的提交"按钮时.
我不希望检查提交按钮.
I wasn't expecting the submit button check.
希望这对某人有帮助
这篇关于远程验证停止提交按钮保存数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!