If my model have
[DisplayName("First Name")]
public string firstName { get; set; }
Then I can print it in the View with LabelFor
@Html.LabelFor(model => model.acc_first)
It will then render as
<label for="firstName">First Name</label>
- But how can I "raw" read the property (FirstName) attributes in the View? If for example, I want to send the value to a function on the View page
To access the attributes you'll need a custom
Html
helper. Since the attributes aren't really part of the property or model you need to go in a round about way to access them.For additional attributes that aren't part of the DataAnnotations you can do the following:
Create a custom attribute
The magic happens in the OnMetadataCreated implementation. Here we can populate the AdditionalValues with anything we need for our particular Attribute. In this case we’re adding a Tooltip key. Your name should be unique as other attributes or providers may add their own keys with the same name It is important to remember that attributes are not always read in the same order. So your tooltip attribute may be called first, last or somewhere in the middle. This is an important distinction as it may cause undesired effects.
Then create a custom Attribute helper
@(This.Model.acc_first)
Should work
so in javascript you can use it like
you should simply use
should work for you. If not, try just
Either one should work fine.
You can access the value of the attribute
Display(Name="...")
in 4 steps:Then
displayname
will have the valueFirst Name
in your context.