2016-06-11 125 views
0

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

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

@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> 
+2

你有'的jQuery .validate.unobtrusive.js'在视图或布局? –

+0

是的,我有我的布局。我查看页面源代码。它包括在内。 – CodeLover

+0

最好的猜测是它没有正确加载,因为表单在工作时不会提交。它是为了 - jQuery然后jquery.validate然后jquery.validate.unobtrusive?无论你是否已禁用客户端验证 –

回答

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"/> 
+0

感谢您的回答。我在布局中添加了我的包,并且包含在我的每个表单中。除了这一个,每个表单都工作正常。 – CodeLover

+0

噢另一件事...有没有机会在第三个下拉菜单中显示不显眼的作品,而不是其他两个?我看到的唯一的其他区别(假设jQuery和验证命令都是犹太教 - 我总是搞砸了)是下拉列表1和2是在运行时动态生成的 - 所以也许不引人注意的值没有验证的值?我不知道这将如何影响他们的需求..我想我需要去睡 –

+0

所有三个下拉不起作用,但有一个下拉动态生成。 – CodeLover