ASP MVC 2 数据注释不适用于 VS2008/ASP 3.5?

发布于 2024-09-13 09:29:08 字数 1157 浏览 3 评论 0原文

我已将数据注释添加到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

林空鹿饮溪 2024-09-20 09:29:08

好吧,缺少一些东西...

我没有将类属性声明为公共,所以数据注释当然失败了。

上面代码片段的最后一行应该是:

public string ProjectDescription {get; set;}

最小的东西......

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:

public string ProjectDescription {get; set;}

The smallest things...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文