Template with Required Attribute Knowledge
This seems very basic but I'm struggling to find a solution I'm happy with at the minute. All I want to do is add a class with the name "Required" to any fields that have a required property against them so:
public class Dummy{
[Required]
public string Name {get; set;}
}
Would be called with
@Html.EditorFor(model=>model.Name)
and would output something like
<input id="Name *Data-VAL and other attribs* class="Required" />
With all t开发者_运维问答he inbuilt unobtrusive goodness etc. I'm using Razor and MVC 3. Any help much appretiated
You may take a look at the following blog post which explains how you could write a custom DataAnnotationsModelMetadataProvider to achieve this.
using System.ComponentModel.DataAnnotations; //You need to import this namespace
public class Dummy {
[Required]
public string Name { get; set; }
}
Further reference: http://www.asp.net/mvc/tutorials/validation-with-the-data-annotation-validators-cs
精彩评论