MVC3 需要验证,选择值为空
我有一个隐藏字段,它绑定到模型中的 int Id,它有一个必需的属性和一些花哨的 ajax 代码来设置 id 客户端,问题是零应该被视为空。现在,即使没有选择 Id,验证也会成功,请问我可以设置哪个值应计为空吗?我希望我不需要为其创建自定义验证器。
谢谢
I have a hidden field which is bound to a int Id in the model, it has a required attribute and some fancy ajax code to set the id client side, the problem is that zero should be acounted as empty. Now the validation will succeed even if no Id has been selected, bow can I set which value should be counted as empty? I hope i do not need to create a custom validator for it.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将必需的属性添加到不可为 null 的类型(例如 Int32)是没有意义的。值类型始终是必需的。您可以使用可为空的整数来代替:
It doesn't maker sense to add required attribute to a non nullable type such as Int32. Value types are always required. You could use a nullable integer instead: