我自己的JavaScript验证+ MicrosoftMvcValidation。是否可以 ?怎么样 [英] My Own javascript validation + MicrosoftMvcValidation . Is it possible ? How

查看:121
本文介绍了我自己的JavaScript验证+ MicrosoftMvcValidation。是否可以 ?怎么样的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想是能够使用MVC验证+我的自定义JavaScript验证。

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.

例如:案例与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进行模型验证。

As you can see I Use ValidationMessageFor for model validation.

EX:自定义验证。

    在这里,我想在列表框进行验证。我想这是必需的。因为这不是强类型的,我需要一个其他的方式,使验证。

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>



我想要的:
如果我点击提交按钮,我想内置的MVC验证+我的自定义验证在同一时间......我的意思是,如果第一个验证(MVC一个)是无效的,我想我的自定义validaiton履行验证了。

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.

感谢

推荐答案

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

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.

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

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).

这篇关于我自己的JavaScript验证+ MicrosoftMvcValidation。是否可以 ?怎么样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆