My Own javascript validation + MicrosoftMvcValidation . Is it possible ? How

戏子无情 提交于 2019-12-13 05:40:55

问题


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


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



来源:https://stackoverflow.com/questions/5314158/my-own-javascript-validation-microsoftmvcvalidation-is-it-possible-how

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!