远程验证阻止提交按钮保存数据

本文关键字:提交 按钮 保存 数据 程验证 验证 | 更新日期: 2023-09-27 17:49:17

我在一个视图上使用远程验证,它工作得很好。我遇到的问题是,当我点击提交按钮时,什么都没有发生,焦点返回到我远程验证的字段-表单永远不会提交。

要设置远程验证,我参考了这个页面- https://msdn.microsoft.com/en-us/library/gg508808(VS.98).aspx -不确定是否有任何最近可用的

如果我从模型中删除[Remote],它会正确提交。我做错了什么?

我的模型是

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

中添加了以下内容
<appSettings>
  <add key="ClientValidationEnabled" value="true" />
  <add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>

远程验证阻止提交按钮保存数据

在看了一个问题2天后,我在提出问题5分钟后就找到了解决方案!哎!

我好像把双重否定搞混了。

在我的控制器中,我有true和false如果你把它们调换一下,它会像预期的那样工作。

我没有想到的一件事是远程验证似乎被激活了两次

  1. 当您离开字段
  2. 当您按下表单上的提交按钮时。

我没想到会有提交按钮检查。

希望这能帮助到一些人

我也面临同样的问题在我的代码…我使用。net Core 3.1 MVC架构

我有以下场景在我的代码,你可以联系到1.我也有一个表单与模型中的远程验证。2.提交按钮调用远程验证在第一次点击,然后在第二次点击表单是张贴。

我发现我在提交按钮中添加了一个Id,就像

 <button type="submit" id="submit" class="btn btn-success">Submit
 </button>

我将提交按钮的Id重命名为其他东西,现在它正在工作,我保留了提交按钮的Id,因为我需要它用于该按钮的一些点击操作。

希望我帮到你。