2011-03-15 47 views
0

希望能够使用MVC验证+我的自定义JavaScript验证。
MVC的验证是非常好的模型验证。这里的主要问题是我有更复杂的验证。我自己的JavaScript验证+ MicrosoftMvcValidation。可能吗 ?我将如何

例:案例与MVC验证

<div class="editorSmall"> 
    <div class="editor-label bold"> 
     <%: Html.LabelFor(model => model.Location.CurID)%>: 
    </div> 
     <div class="editor-field"> 
     <%:Html.DropDownListFor(model => model.Location.CurID, Model.CurrenciesList)%> 
     <%: Html.ValidationMessageFor(model => model.Location.CurID)%> 
    </div> 
</div> 

<div class="editorSmall"> 
    <div class="editor-label bold"> 
     <%: Html.LabelFor(model => model.Location.UnitID)%>: 
    </div> 
     <div class="editor-field"> 
     <%:Html.DropDownListFor(model => model.Location.UnitID,Model.UnitList)%> 
     <%: Html.ValidationMessageFor(model => model.Location.UnitID)%> 
    </div> 
</div> 

正如你可以看到我使用ValidationMessageFor模型验证。

EX:自定义验证。
在这里,我想在列表框进行验证。我希望它是必需的。因为这不是强类型的,所以我需要其他方法来进行验证。

<div class="editorSmall" > 
     <div class="editor-label bold"> 
      <label><%:Model.GrpName1%>:</label> 
     </div> 
     <div class="editor-field"> 
      <%: Html.ListBox("Model_Groupe1", new MultiSelectList(Model.Groupe1, "GrpDescID", "GrpDescTxt", Model.Groupe1Selected.Select(g => g.GrpDescID)), new { @class = "grplb" })%> 
     </div> 
    </div> 


我想要什么: 如果我点击提交按钮,我想建在MVC验证+在同一时间我自定义的验证......我的意思是,如果第一次验证(MVC之一)无效,我希望我的自定义验证也可以执行验证。

感谢

回答

0

能想到几个不同势的方式来处理这个问题的,但是,正如你可能已经想到了,你的自定义脚本验证脚本,如果未在客户端上启用将无法运行。我猜这不是问题?

在这种情况下: 覆盖提交按钮,做您的验证逻辑之前提交。把结果放在隐藏的输入中。 将隐藏的输入添加到模型中,并验证该输入而不是选择。

然后,您可以使用validationmessage您隐藏的输入来显示错误或回传后再次运行逻辑(如果你有想你的脚本显示validationmessages)。

相关问题