MVC验证与jQuery手风琴 [英] MVC Validation with JQuery Accordion

查看:121
本文介绍了MVC验证与jQuery手风琴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是比较新的ASP.NET MVC,但我的验证功能pssed真的IM $ P $。我有我已经分裂成使用JQuery UI手风琴一个巨大的形式。这种形式是一种模式的一个非常基本的支架和模型有code像下面来验证每个为必填字段:

I am relatively new to ASP.NET MVC but am really impressed by the validation features. I have a giant form that I have split into an accordion using JQuery UI. This form is a very basic scaffold of a model and that model has code like the following to validate each field that is required:

    [Required(ErrorMessage="Please Enter a Valid Product")]
    [DisplayFormat(ConvertEmptyStringToNull= false)]
    [StringLength(160, MinimumLength=2,ErrorMessage="Product Must Be At Least 2 Characters")]

和则认为只需有:

            <div class="editor-label">
                @Html.LabelFor(model => model.Product)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Product)
                @Html.ValidationMessageFor(model => model.Product)
            </div>

这如果该字段在手风琴正在查看的伟大工程。如果你离开领域的空白,消息显示出来,一切都会按计划进行。问题是,当我切换到手风琴不同的选项卡,以及所需的领域是不可见的,我可以提交表单就好了,多到我的服务器沮丧。如果我填写领域,如预期当然它传递。有没有办法来解决这个问题?

That works great if that field is being viewed in the accordion. If you leave the field blank, the messages shows up and everything works as planned. Problem is, when I switch to a different tab in the accordion, and the required field isn't visible, I can submit the form just fine, much to the dismay of my server. If I fill out the field, it is passed as expected of course. Is there a way to get around this?

推荐答案

原来,答案是把下列观点:

Turns out the answer was to put the following into the view:

$('#Form').validate().settings.ignore = []

问题是,隐藏字段不使用jQuery验证(这是MVC使用),并曾经是一个手风琴折叠它们被隐藏检查。这告诉他们不要忽略它们。

The issue was that hidden fields are not checked using Jquery validation (which is what MVC uses) and once an accordian collapses they are hidden. This tells them not to ignore them.

这篇关于MVC验证与jQuery手风琴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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