[Required] [StringLength(6,MinimumLength=3)] [Display(Name="User Name")] [RegularExpression(@"(\S)+",ErrorMessage="White space is not allowed")] [ScaffoldColumn(false)] publicstringUserName{get;set;}
the [ ] means that it make client and server validation.
That makes sense to me when i use it to define a atribute.
But when i use it inthe first part of the class definition it means that those are the default values for every atribute of the class?
[Required] [StringLength(6,MinimumLength=3)] [Display(Name="User Name")] [RegularExpression(@"(\S)+",ErrorMessage="White space is not allowed")] [ScaffoldColumn(false)]
I
am sorry if its too dumb, i started MVC yesterday
An attribute is a C# construct that lets you decorate either a class, method or property with some code. The idea is to remove cross cutting code that can be shared in many places. Check out
Aspect Oriented Programming for more information.
In terms of MVC validation, Data Annonation attributes are used to decorate the model. Essentially you are decorating properties with you validation rules (Required, StringLength, etc...). These rules apply to properties only, try putting them on a class,
it won't let you because these attributes are for properties only (there are attributes that can be placed on classes).
Each set of attributes correspond to the property underneath it. So for example
[Required]
[StringLength(6, MinimumLength = 3)]
[Display(Name = "User Name")]
[RegularExpression(@"(\S)+", ErrorMessage = "White space is not allowed")]
[ScaffoldColumn(false)]
public string UserName { get; set; }
All those attributes below to the username property and basically tells MVC that the UserName property is required, should be between 3 and 6 characters, has a displa name of "User Name" etc.
Now this on the otherhand defines the attributes fo the firstname property , because again it's the property that comes after all the annonations.
But when i use it inthe first part of the class definition it means that those are the default values for every atribute of the class?
[Required] [StringLength(6,MinimumLength=3)] [Display(Name="User Name")] [RegularExpression(@"(\S)+",ErrorMessage="White space is not allowed")] [ScaffoldColumn(false)]
No,
You can't use these attributes on the class level, they can be used only for properties / fields.This is because they are decorated with class usage attribute that are configured for properties /fields.
Example, if you look at the
msdn definiton of the [Required] attribute, you will notice this line in the attribute declaration:
anderson7777
Member
141 Points
279 Posts
I am having trouble to understand this class definition..
Apr 07, 2012 08:32 PM|LINK
the [ ] means that it make client and server validation.
That makes sense to me when i use it to define a atribute.
But when i use it inthe first part of the class definition it means that those are the default values for every atribute of the class?
[Required]
[StringLength(6, MinimumLength = 3)]
[Display(Name = "User Name")]
[RegularExpression(@"(\S)+", ErrorMessage = "White space is not allowed")]
[ScaffoldColumn(false)]
I am sorry if its too dumb, i started MVC yesterday
thx
anderson
ignatandrei
All-Star
134527 Points
21579 Posts
Moderator
MVP
Re: I am having trouble to understand this class definition..
Apr 08, 2012 04:08 AM|LINK
No , you can put
[StringLength(600, MinimumLength = 30)]
CodeHobo
All-Star
18647 Points
2647 Posts
Re: I am having trouble to understand this class definition..
Apr 08, 2012 05:18 PM|LINK
An attribute is a C# construct that lets you decorate either a class, method or property with some code. The idea is to remove cross cutting code that can be shared in many places. Check out Aspect Oriented Programming for more information.
In terms of MVC validation, Data Annonation attributes are used to decorate the model. Essentially you are decorating properties with you validation rules (Required, StringLength, etc...). These rules apply to properties only, try putting them on a class, it won't let you because these attributes are for properties only (there are attributes that can be placed on classes).
Each set of attributes correspond to the property underneath it. So for example
[Required] [StringLength(6, MinimumLength = 3)] [Display(Name = "User Name")] [RegularExpression(@"(\S)+", ErrorMessage = "White space is not allowed")] [ScaffoldColumn(false)] public string UserName { get; set; }All those attributes below to the username property and basically tells MVC that the UserName property is required, should be between 3 and 6 characters, has a displa name of "User Name" etc.
Now this on the otherhand defines the attributes fo the firstname property , because again it's the property that comes after all the annonations.
[Required] [StringLength(8, MinimumLength = 3)] [Display(Name = "First Name")] public string FirstName { get; set; }Blog | Twitter : @Hattan
Gaspard
Contributor
2066 Points
416 Posts
Re: I am having trouble to understand this class definition..
Apr 08, 2012 05:28 PM|LINK
Further information on attributes at this web page
http://blogs.msdn.com/b/aspnetue/archive/2010/02/24/attributes-and-asp-net-mvc.aspx
Regards
anas
All-Star
73649 Points
7914 Posts
Moderator
Re: I am having trouble to understand this class definition..
Apr 08, 2012 05:56 PM|LINK
No,
You can't use these attributes on the class level, they can be used only for properties / fields.This is because they are decorated with class usage attribute that are configured for properties /fields.
Example, if you look at the msdn definiton of the [Required] attribute, you will notice this line in the attribute declaration:
It means that the usage of this attribute is restricted to Property or Field only.
The same applies for the rest of the mentioned attributes.