Attribute 'DisplayColumn' is not valid on this declaration type. It is only valid on 'class' declarations
Just wondering why I get compile time error :
"Attribute 'DisplayColumn' is not valid on this declaration type. It is only valid on 'class' declarations."
开发者_如何转开发
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace MyApplication.Models.DataAnnotations
{
[MetadataType(typeof(AppUser_DataAnnotations))]
public partial class AppUser
{
}
public class AppUser_DataAnnotations
{
[DisplayColumn("Name")]
public string FirstName { get; set; }
}
}
I'm using above to override mvccontrib rendered grid column headers. Any idea why I get compile time error? Any help would be greatly appreciated.
For people who run into this problem in the future maybe this can help:
I got the same problem as described above. As described in other answers it is important to use the DisplayName property and to include the System.ComponentModel namespace. Besides that, the property must have a getter (and setter) to get the attribute working. The errormessage is a bit confusing on this one.
The reason you are getting a compile-time error is because the [DisplayColumn]
attribute can only be applied at the class level and not at a property of the class. You are probably confusing this attribute with [DisplayName]
.
You can also run into this problem when you decorate a Model Property with the attribute.
You need to decorate Controller Method's with ActionFilterAttribute
's not property's.
精彩评论