ASP MVC 2 数据注释不适用于 VS2008/ASP 3.5?
我已将数据注释添加到 MS 指南中引用的“伙伴”类中。特别是,[DisplayName ("Name")]
似乎没有任何影响。我的理解是,分配给注释的值应该由 Html.LabelFor(m => m.Attribute) 帮助器引用和使用来显示字段的标签。
我在这件事上有错吗?
我注意到在视图数据类中有一个名为 EntityName+EntityName_Validation 的强类型视图。这是否需要额外的组件?
我尝试使用其中一种类型创建视图,但结果视图中不存在脚手架。也许这完全是一个不同的话题。 UIHint
似乎也没有任何影响。
如前所述,这是 VS2008 中的 ASP.NET 3.5 代码。我正在使用 Linq to SQL。也许这也取消了数据注释的充分使用。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace Sample.Models {
[MetadataType (typeof (SampleRequest_Validation))]
public partial class SampleRequest {
public class SampleRequest_Validation {
[DisplayName ("Description of Project:")]
[Required (ErrorMessage = "Project description is required.")]
[StringLength (500, ErrorMessage = "Project description cannot exceed 500 characters.")]
[UIHint ("TextArea")]
string ProjectDescription {get; set;}
我怀疑我在某处缺少一些参考......
谢谢!
I have added data annotations to a 'buddy' class as referenced in MS guidance. In particular, the [DisplayName ("Name")]
does not seem to be affecting anything. My understanding is that the value assigned to the annotation should be referenced and used by the Html.LabelFor(m => m.Attribute)
helper to display the label for the field.
Am I wrong on this?
I noticed in the view data class there is a strong-type view called EntityName+EntityName_Validation. Does that pull in additional components needed?
I tried creating a view using one of those types and there is no scaffolding present in the resulting view. Perhaps that is a different topic altogether. The UIHint
doesn't seem to have any impact either.
As mentioned, this is ASP.NET 3.5 code, in VS2008. I am using Linq to SQL. Perhaps this disqualifies the full use of data annotations too.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace Sample.Models {
[MetadataType (typeof (SampleRequest_Validation))]
public partial class SampleRequest {
public class SampleRequest_Validation {
[DisplayName ("Description of Project:")]
[Required (ErrorMessage = "Project description is required.")]
[StringLength (500, ErrorMessage = "Project description cannot exceed 500 characters.")]
[UIHint ("TextArea")]
string ProjectDescription {get; set;}
I suspect I am missing some reference somewhere...
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,缺少一些东西...
我没有将类属性声明为公共,所以数据注释当然失败了。
上面代码片段的最后一行应该是:
最小的东西......
Okay, there was something missing...
I didn't declare the class attibutes as public, so of course the data annotations failed.
The last lisne from the code snippet above should read:
The smallest things...