HTML and CSS Reference
In-Depth Information
[StringLength(20, MinimumLength = 6)]
public string Password { get; set; }
[StringLength(50, MinimumLength = 10)]
[DisplayName("Blog / Website")]
public string BlogUrl { get; set; }
[StringLength(500,ErrorMessage = "Bio size out of permissible limits.")]
[DisplayName("Proile")]
public string Bio { get; set; }
[Required]
[DisplayName("Legal Age")]
public string Age { get; set; }
[Required]
[DisplayName("Yearly Income")]
public string Income { get; set; }
}
[MetadataType(typeof(UserMetadata))]
public partial class User
{
}
As you can see, the
UserMetadata
class contains property definitions matching the
User
model class.
The properties are then decorated with data annotation attributes such as
[Required]
,
[StringLength]
,
and
[DisplayName]
. As you might have guessed, the
[Required]
attribute ensures that a property value is
set. The
[StringLength]
attribute lets you specify minimum and maximum lengths for a string property.
The
[DisplayName]
attribute is used to specify friendlier names for the properties. These names are used in
the server-side error messages instead of the actual property names.
The
UserMetadata
class is a standalone class. To link it with the
User
model class, you need to create a
User
partial class and decorate it with
[MetadataType]
attribute.
n
Note
Data annotation attributes do a great job of implementing common validation criteria at the model level.
ASP.NET validation mechanisms then use this information to provide the user with visual feedback about validation
errors. A detailed discussion of data annotation attributes is beyond the scope of this topic.
User Controller
The user registration application has a single controller (
User
) that contains two variations of
Index()
action methods. These action methods are shown in Listing 5-27.
Listing 5-27.
Index Action Methods of the
User
Controller
public ActionResult Index()
{
return View();
}