我自己的 javascript 验证 + MicrosoftMvcValidation 。是否可以 ?如何
我希望能够使用 MVC 验证 + 我的自定义 javascript 验证。
MVC 验证对于模型验证来说非常有用。这里的主要问题是我有更复杂的验证。
示例: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 一个)无效,我希望我的自定义验证执行他的验证也。
谢谢
I would like to be able to use the MVC validation + my custom javascript validation.
The MVC validation is really nice for Model Validation. The main problem here, is that I have more complexe validation.
Ex: Case with MVC Validation
<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>
As you can see I Use ValidationMessageFor for model validation.
EX: Custom Validation.
Here I want to perform a validation on the listbox. I Want it to be Required. Because this is not strongly typed, I need an other way to make the validation .
<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>
What i want :
If I click on the Submit Button, I want the built in MVC Validation + my custom validation at the same time... What I Mean is, if the First Validation(MVC one) is invalid, I want my custom validaiton perform his validation too.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可以考虑几种不同的方法来处理这个问题,但是,正如您可能已经想到的那样,如果客户端上未启用脚本,您的自定义脚本验证将不会运行。我想这不是问题吧?
在这种情况下:
覆盖提交按钮并在提交之前执行验证逻辑。将结果放入隐藏输入中。
将隐藏输入添加到模型中并验证它而不是选择。
然后,您可以使用隐藏输入的验证消息来显示错误或在回发后再次运行逻辑(如果您希望脚本显示验证消息)。
Could think of a couple of diffrent ways to handle this but, as you probably already have thought of, your custom script validation will not run if script is not enabled on the client. I'm guessing that's not a problem?
In that case:
Override submitbutton and do your validation logic before submit. Put the result in hidden input.
Add the hidden input to the model and validate that instead of the select.
You could then use the validationmessage for your hidden input to display errors or run the logic again after postback (if you have want your script to display the validationmessages).