ASP.Net MVC - Use the AdditionalMetadata attribute to pass additional data to the views.

By [)ia6l0 iii

The [AdditionalMetadata] attribute is a powerful feature of MVC 3. You can make use of this attribute and pass additional data that will help you make judgements in scenarios where you need to display/take decisions conditionally. The following example makes it more clear.

We have added the additional attribute called "Private" which defines if the contact name is private or not.

[AdditionalMetadata("Private", "true")]
public string ContactName { get; set; }

You can retrieve the metadata values using the ViewData object as shown below:

object _isPrivate;
ViewData.ModelMetadata.AdditionalValues.TryGetValue("Private", out _isPrivate);

Related FAQs

You can pass the master page name when you return the ViewResult from the Controller's Action.
Use the ActionLink overloaded method that takes the html attributes that would be applied to the generated link.
There are many threads on the internet that tell you to play with Session or any other web.config values to get Global.asax events working. If they do not work, then this tip is for you.
To stop a robot from visiting your website and causing undesirable robot traffic and many other problems, follow one of the below said three ways:
One of the best ways is to use the RouteConstraint to validate the route values and then choose a route to controller. Route values that hold values of integer is the best example. If the route value is passed as string which cannot be converted to an Integer, we suffer with run-time and unhandled exceptions on our code.
Use Page.ClientQueryString to get the encoded query string portion of the requested url.
ASP.Net MVC - Use the AdditionalMetadata attribute to pass additional data to the views.  (674 Views)
Create New Account