【问题标题】:MVC immediate validation is not triggering?MVC 立即验证未触发?
【发布时间】:2016-06-11 07:44:00
【问题描述】:

我在 MVC 5 中有一个表单。我的表单上有三个必需的下拉菜单。当我提交表单时,出现错误消息。问题是,当我从下拉列表中选择值时,错误消息仍然显示。然后,如果我提交按钮,所需的错误消息就会消失。

主要问题是立即验证不起作用。当我第一次提交没有选择的表单然后我选择下拉值时,就会发生这种情况。错误仍然显示。 这是我的表格。

@using (Html.BeginForm())
{

@Html.AntiForgeryToken()
@Html.HiddenFor(model => model.ProcessId)


<div class="row">
    <div class="col-sm-6">
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    </div>
</div>


<div class="row">
    <div class="col-sm-12 bgnMrgn">
        <div class="col-sm-12">
            <h3>@Resources.BasicInformtion</h3>
        </div>


        <div class="form-group col-sm-2">
            <label class="control-label">Intiqal Type</label>
            @Html.DropDownListFor(model => model.Intiqal.IntiqalTypeId, new SelectList(Model.IntiqalTypes, "IntiqalTypeId", "IntiqalTypeName"), Resources.Select, new { id = "intiqalTypes", Class = "form-control" })
            @Html.ValidationMessageFor(model => model.Intiqal.IntiqalTypeId, "", new { id = "", @class = "text-danger" })



        </div>
        <div class="form-group col-sm-2">
            <label class="control-label">Source</label>
            @Html.DropDownListFor(model => model.Intiqal.ProcessInitiationTypeId, new SelectList(new List<ProcessInitiationType>
            (), "ProcessInitiationTypeId", "ProcessInitiationTypeName"), Resources.Select, new { id = "processInitiationTypes", Class = "form-control" })
            @Html.ValidationMessageFor(model => model.Intiqal.ProcessInitiationTypeId, "", new { id = "", @class = "text-danger" })
        </div>

        <div class="form-group col-sm-2 hidden" id="shamilatSelection">
            <label class="control-label">Shamilat</label>
            @Html.DropDownListFor(model => model.Intiqal.Shamilat, new SelectList(Model.Shamilat, "Key", "Value"), Resources.Select, new { id = "shamilat", Class = "form-control " })
            @Html.ValidationMessageFor(model => model.Intiqal.Shamilat, "", new { @class = "text-danger" })
        </div>
    </div>
</div>

//Other stuff including submit button
    }

有什么问题?

已编辑——这是脚本顺序

<script src="/Scripts/jquery-2.2.3.js"></script>
<script src="/Scripts/jquery-ui-1.11.4.min.js"></script>

<script src="/Scripts/jquery.unobtrusive-ajax.js"></script>
<script src="/Scripts/jquery.validate.js"></script>
<script src="/Scripts/jquery.validate.unobtrusive.js"></script>

【问题讨论】:

  • 视图或布局中是否有jquery.validate.unobtrusive.js
  • 是的,我的布局中有。我查看页面源。它包括在内。
  • 最好的猜测是它没有正确加载,因为如果它正在工作,表单将不会提交。是否按顺序排列 - jquery 然后 jquery.validate 然后 jquery.validate.unobtrusive?你们中的任何一个都禁用了客户端验证
  • @StephenMuecke 我用脚本顺序编辑了问题。请看。
  • 看起来不错。您确定没有在视图中包含任何重复项吗?

标签: javascript jquery asp.net-mvc forms


【解决方案1】:

在我创建的几乎每个 MVC 项目中,我似乎都在努力解决这个问题。

问题可能是您没有加载(或以错误的顺序加载等)该表单的 jquery-validation-unobtrusive.js(或 .min.js)。

我通常会确保将其添加到我的 ScriptBundle 包中,以便在每个页面上都可以进行不显眼的验证。

在很多情况下,我不得不再次为 jquery 不显眼的验证包卸载包和安装包。

除此之外,在带有表单的页面上,我通常将其中一个放在底部:

@section Scripts 
{
@Scripts.Render("~/bundles/jqueryval")
}

并确保您的 web.config 包含以下内容:

 <appSettings>
<add key="ClientValidationEnabled" value="true"/>
<add key="UnobtrusiveJavaScriptEnabled" value="true"/>

【讨论】:

  • 感谢您的回答。我在布局中添加了我的包并包含在我的每个表单中。除了这个之外,每个表单都可以正常工作。
  • Ooo 另一件事......有没有可能不显眼的工作在第三个下拉菜单上而不是其他 2?我看到的唯一其他区别(假设 jquery 和验证顺序都是 kosher 的——我总是把它搞砸)是下拉列表 1 和 2 是在运行时动态生成的——所以也许不显眼的没有要验证的值?我不确定这会如何影响他们被要求..我想我需要睡觉了
  • 三个下拉菜单都不起作用,但动态生成了一个下拉菜单。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-23
  • 2022-10-07
  • 2016-06-03
  • 1970-01-01
  • 1970-01-01
  • 2021-03-31
相关资源
最近更新 更多