具有所需属性知识的模板
这看起来非常基本,但我正在努力寻找目前令我满意的解决方案。我想要做的就是将一个名为“Required”的类添加到任何具有必需属性的字段中,这样:
public class Dummy{
[Required]
public string Name {get; set;}
}
将被调用
@Html.EditorFor(model=>model.Name)
并输出类似“
<input id="Name *Data-VAL and other attribs* class="Required" />
具有所有内置的不引人注目的优点”等。我正在使用 Razor 和MVC 3.非常感谢任何帮助
This seems very basic but I'm struggling to find a solution I'm happy with at the minute. All I want to do is add a class with the name "Required" to any fields that have a required property against them so:
public class Dummy{
[Required]
public string Name {get; set;}
}
Would be called with
@Html.EditorFor(model=>model.Name)
and would output something like
<input id="Name *Data-VAL and other attribs* class="Required" />
With all the inbuilt unobtrusive goodness etc. I'm using Razor and MVC 3. Any help much appretiated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以查看 以下博客文章解释了如何编写自定义DataAnnotationsModelMetadataProvider 来实现这一点。
You may take a look at the following blog post which explains how you could write a custom DataAnnotationsModelMetadataProvider to achieve this.
进一步参考:http://www.asp. net/mvc/tutorials/validation-with-the-data-annotation-validators-cs
Further reference: http://www.asp.net/mvc/tutorials/validation-with-the-data-annotation-validators-cs