MVC3 / 4验证隐藏字段 [英] MVC3/4 Validating Hidden Fields

查看:196
本文介绍了MVC3 / 4验证隐藏字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经建立了模型验证我的形式,但验证似乎并没有在所有的工作。我不认为任何人都可以提供帮助。我一直在使用,工作围绕以下尝试,但,保持在Firebug拉起一个未定义错误。

I have set up model validation for my form but validation doesn't seem to work at all. I don't suppose anybody can help. I've tried using the below work-around but that keeps pulling up an 'undefined' error in firebug.

示例工作围绕脚本:

<script type="text/javascript">
    $(document).ready(function () {            
        $.validator.setDefaults({ ignore: [] });
    });
</script>

示例文本字段(注:客户名称自动填充文本字段):

    @Html.TextBox("txtCustomer", null, new { @id = "txtCustomer", @class = "txt" })

示例隐藏字段(注:当选择从自动完成领域取得的ID注入隐藏字段):

    @Html.HiddenFor(model => model.Customer_ID, new { @id = "hCustomerID" })

示例模型数据注释

    [Range(1, Int32.MaxValue, ErrorMessage = "Please select a customer")]
    public int Customer_ID { get; set; }

编辑:错误的屏幕截图

对不起,我要发布链接的截图,因为我代表分够高。

Sorry i have to post links to the screenshots because my rep points are high enough.

UI回发错误

编辑:屏幕截图显示的页面的源代码

页面源代码的屏幕截图

推荐答案

为时已晚,以改变在不显眼的脚本之后,这种方式验证程序忽略。这是因为验证只在默认一次拉 - 创建时。不显眼的脚本为您创建验证器。您需要引用现有的验证对象,并对其进行更新。

Its too late to change the ignore on the validator in this way after the unobtrusive script. This is because the validator only pulls in the defaults once - when it is created. The unobtrusive script creates the validator for you. You need to reference the existing validator object and update it.

试试这个

<script>
    $(document).ready(function () {            
        $('form').validate().settings.ignore = []
    });
</script>

这篇关于MVC3 / 4验证隐藏字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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