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

时间:2016-06-24 02:07:10

标签: c# asp.net asp.net-mvc validation

我在视图上使用远程验证,效果很好。我遇到的问题是当我点击提交按钮时没有任何反应,焦点会返回到我有远程验证的字段 - 表单永远不会被提交。

要设置远程验证,我引用了此页面 - 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 个答案:

答案 0 :(得分:1)

在查看问题2天后,我在提出问题5分钟后发现了解决方案!卫生署!

似乎我的双重否定感到困惑。

在我的控制器中,如果你切换它我是真的和假的,那么它会按预期工作。

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

  1. 离开现场时
  2. 当您按表单上的提交按钮时。
  3. 我没想到提交按钮检查。

    希望这有助于某人

答案 1 :(得分:0)

我的代码中也遇到了同样的问题... 我正在使用.net Core 3.1 MVC架构

我的代码中有以下情况,您可以将其与 1.我还有一个带有“模型中的远程验证”的表格。 2.Submit按钮调用该远程验证,首先单击,然后再次单击,正在发布表单。

所以我发现我像下面这样在提交按钮中添加了一个ID

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

我将“提交ID”按钮重命名为其他名称,现在它可以正常工作了,我将ID保留在“提交”按钮上,因为在该按钮的“单击”操作中我需要它。

希望我能帮上忙。