1

我有一个供应商实体,其中包含

ID - int
Status - string
Name - string
CreateDate- datetime

我正在使用部分类方法为上述实体创建数据注释。如此处所述

[MetadataType(typeof(SupplierMetadata))]
public partial class Supplier
{
    // Note this class has nothing in it.  It's just here to add the class-level attribute.
}

public class SupplierMetadata
{
    // Name the field the same as EF named the property - "FirstName" for example.
    // Also, the type needs to match.  Basically just redeclare it.
    // Note that this is a field.  I think it can be a property too, but fields definitely should work.

    [HiddenInput]
    public Int32 ID;

    [Required]
    [UIHint("StatusList")]
    [Display(Name = "Status")]
    public string Status;

    [Required]
    [Display(Name = "Supplier Name")]
    public string Name;
}

HiddenInput 注释会引发错误,指出“属性‘HiddenInput’在此声明类型上无效。它仅在‘类、属性、索引器’声明上有效。”

请帮忙

4

2 回答 2

4

该错误指出该属性只能添加到“类、属性或索引器声明”。

public Int32 ID;这些都不是——它是一个领域。

如果您将其更改为属性

public Int32 ID { get; set; }您将能够使用该属性来装饰它。

于 2012-06-14T20:46:03.757 回答
1

您的所有属性都定义不正确,因为它们缺少 get/set 访问器。.NET 中的所有属性都需要 getter 和/或 setter 访问器。将您的代码更改为:

public class SupplierMetadata
{
    [HiddenInput]
    public Int32 ID { get; set; }

    [Required]
    [UIHint("StatusList")]
    [Display(Name = "Status")]
    public string Status { get; set; }

    [Required]
    [Display(Name = "Supplier Name")]
    public string Name { get; set; }
}
于 2012-06-15T01:15:04.993 回答