There is an ability to create fields, fields labels and fields validation basing on model attributes in ASP.Net MVC:
public class SampleModel
{
[StringLength(50)]
[Required(AllowEmptyStrings = false)]
[DisplayName("Test")]
public object Name { get; set; }
}
In this case while you creating page you can use helpers:
@Html.LabelFor(m => m.Name)
@Html.TextBoxFor(m => m.Name)
@Html.ValidationMessageFor(m => m.Name)
You will get HTML form:
Everything will work with Sitecore MVC. However if you need provide translation of labels and messages on you form in Sitecore way you need to customize model attributes.
For DisplayNameAttribute it is easy to achieve with inheriting from standard attribute with adding Translate.Text:
public class DisplayNameAttribute : System.ComponentModel.DisplayNameAttribute
{
public DisplayNameAttribute(string displayName)
: base(displayName)
{
}
public override string DisplayName
{
get
{
return Translate.Text(base.DisplayName);
}
}
}
In this case when you will use new attribute for you model you will receive translated text for your labels. Same thing could be done for ValidationMessageFor, Required and other attributes.
No comments:
Post a Comment