Adding CSS class to field from DataAnnotations MVC 3
Is it possible to add a CSS class from DataAnnotations (metadata) when I use Html.EditorForModel() - without a custom template...
Something like this [DataType(CssClass = "DateTime")]
public class TestIn开发者_如何学Goput
{
[Required(ErrorMessage = "Name is required.")]
public string Name { get; set; }
[Required, StringLength(500)]
[DataType(DataType.MultilineText)]
public string Description { get; set; }
[Required]
[DataType(CssClass = "DateTime")]
public DateTime Date { get; set; }
[HiddenInput(DisplayValue = false)]
public int Id { get; set; }
}
Here is a link to what I am referring to: http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-2-modelmetadata.html
That's an interesting idea. Not sure how to go about implementing an attribute that does that, but I would have to say its possible when you can do things like Display(Name="Display Name")
.
Not sure why youre against templates. Until such a thing is created I think your best option is to create an EditorTemplate for a custom type like "MyDateTime" and use UIHint to indicate. One line of an HTML helper could set the class you're after. Not the best approach for something as simple as classes/reusability but eh for now.
精彩评论