我在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
}
问题是什么?
EDITED - 这是脚本Order
<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>
答案 0 :(得分:0)
这是我似乎在我创建的几乎所有MVC项目中都要解决的问题。
问题可能是你要么没有为该表单加载(或加载错误的顺序等)jquery-validation-unobtrusive.js(或.min.js)。
我通常会确保它已添加到我的ScriptBundle包中,因此不显眼的验证适用于每一页。
在许多情况下,我被迫为jquery unobtrusive验证包再次卸载-package和install-package。
除此之外,在带有表单的页面上,我通常会在底部抛出其中一个:
@section Scripts
{
@Scripts.Render("~/bundles/jqueryval")
}
确保您的web.config包含以下内容:
<appSettings>
<add key="ClientValidationEnabled" value="true"/>
<add key="UnobtrusiveJavaScriptEnabled" value="true"/>