对于要求的属性设置初始值 [英] Set Initial Value for Required attribute

查看:122
本文介绍了对于要求的属性设置初始值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用数据注释来指定一个模型验证规则,我想这样失败,如果它等于该值设置为所需属性的初始值。这是在使用验证控件的WebForms一种选择,但我没有看到类似的使用数据注释。

Using Data Annotations to specify validation rules for a model, I want to set the initial value for the Required attribute so that it fails if it's equal to that value. This was an option in webforms using the validation controls, but I don't see similar using Data Annotations.

如果没有内置的方法来做到这一点,我可能就建立一个新的必需属性继承自它。

If there's no built in way to do it, I'll probably just build a new Required Attribute that inherits from it.

更新:根据意见/答案,我肯定知道这是不是一个理想的情况下,我应该使用HTML5的占位符属性。但是,我必须这样做对现有的形式,已经有阴影的文字被添加在非理想的方式。

UPDATE: Based on comments/answers, I definitely know this is not an ideal situation and I should be using the placeholder attribute of HTML5. However, I need to do this for an existing form that already had shadow text being added in a non-ideal way.

推荐答案

我不会处理在MVC,我想工作,让阴影文本不与形式发布。 HTML 5有占位符。我会使用或执行类似的东西: http://davidwalsh.name/html5-placeholder

I wouldn't handle that in MVC, I'd work on getting the shadow text to not post with the form. HTML 5 has "placeholder". I'd use that or implement something like it: http://davidwalsh.name/html5-placeholder

看一下一个简单快速的占位符方法,适当降低非HTML5的浏览器:

Check this out for a simple quick placeholder approach that degrades properly in non-html5 browsers:

JQuery的占位符HTML5模拟器

占位符属性,只需添加到您的文本框,并使用JavaScript来使其工作的HTML版本不管。

Just add the placeholder attribute to your textboxes and use that javascript to make it work regardless of HTML version.

这篇关于对于要求的属性设置初始值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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