Is there a way to access any attributes (whether data annotation attributes, validation attributes, or user attributes) in the ViewModel properties from the view? One of the things that I would like to add is a bit of the required indicator next to the fields whose property has the [Required] attribute.
For example, if my ViewModel looked like this:
public class MyViewModel { [Required] public int MyRequiredField { get; set; } }
I would like to do something in the EditorFor template like this:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<int?>" %> <div class="label-container"> <%: Html.Label("") %> <% if (PROPERTY_HAS_REQUIRED_ATTRIBUTE) { %> <span class="required">*</span> <% } %> </div> <div class="field-container"> <%: Html.TextBox("") %> <%: Html.ValidationMessage("") %> </div>
source share