Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Example of using AdditionalMetadata in MVC 3

In asp.net MVC 3 there is a new attribute that allows us to pass additional Meta Data to our views e.g.

    [Required]
    [AdditionalMetadata("Tooltip", "The title of the item")]
    public string Title { get; set; }

The question is, how do I actually make use of this information in my view? I thought that perhaps it would render out the data as html 5 data attributes but this is not the case.

A simple example would be much appreciated.

like image 482
Ben Foster Avatar asked Jan 17 '11 16:01

Ben Foster


1 Answers

According to the documentation:

You can use the AdditionalMetadataAttribute class to populate the ModelMetadata.AdditionalValues dictionary for a model property.

...

This metadata is made available to any display or editor template when a product view model is rendered. It is up to you as application developer to interpret the metadata information.

So let's make use of it in the view:

<h2>
    @ModelMetadata.FromLambdaExpression(x => x.Title, ViewData).AdditionalValues["Tooltip"]
</h2>
like image 80
Darin Dimitrov Avatar answered Oct 21 '22 08:10

Darin Dimitrov