8

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.

1
  • AFAIK, you're responsible for rummaging through ModelMetadata.AdditionalValues within your templates to make use of this. Dunno if there are any "default" keys that come in useful out of the box. Commented Jan 17, 2011 at 17:05

2 Answers 2

17

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>
Sign up to request clarification or add additional context in comments.

2 Comments

Would you also show us it working in an Editor Template? - ta
me again, sorry, ViewData.ModelMetadata.AdditionalValues["Tooltip"]
0

example in a template:

Object optionlabelOverride;
ViewData.ModelMetadata.AdditionalValues.TryGetValue("OptionLabelText", out optionLabelOverride);

the variable you are reading must be an object and then you can cast it. Everything you set in the model is available as ViewData.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.