MVC extension DataAnnotationsModelMetadataProvider adds any attribute and value to the page element corresponding to the model attribute

Source: Internet
Author: User

For example, there is a class:

Public class User
{
Public string Name {get; set ;}
}

 

On the strong type view page, the input element corresponding to the attribute Name is displayed, and you want to add a title attribute and the corresponding value,

 

□Ideas

→ Custom TooltipAttribute, which can be attached to the Name attribute.
→ Custom DataAnnotationsModelMetadataProvider: place the Tooltip attribute value of TooltipAttribute into ModelMetadata. The value type is the AdditionalValues attribute of the key-value pair.
→ Write an extension method of HtmlHelper <TModel> to obtain the AdditionalValues attribute value of ModelMetadata.

 

Call the custom feature TooltipAttribute to the property.

Using MvcApplication1.Extension;

Namespace MvcApplication1.Models
{
Public class User
{
[Tooltip ("Enter the user name")]
Public string Name {get; set ;}
}
}

 

Custom DataAnnotationsModelMetadataProvider: place the Tooltip attribute value of the custom feature TooltipAttribute into AdditionalValues of ModelMetadata.

 

Extended HtmlHelper <TModel>. First, obtain the ModelMetadata related to the model, from ModelMetadata. in AdditionalValues, the value of Tooltip is typed in the Name attribute of the User. The value is [Tooltip ("enter User Name")] corresponding to the input parameter title = "enter User Name ".

using System.Linq.Expressions;namespace System.Web.Mvc{    public static class TooltipExtension    {        public static MvcHtmlString TooltipFor<TModel, TValue>(this HtmlHelper<TModel> html,            Expression<Func<TModel,TValue>> expression)        {            var modelMetadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData);            if (modelMetadata.AdditionalValues.ContainsKey("Tooltip"))            {                return new MvcHtmlString((string)modelMetadata.AdditionalValues["Tooltip"]);            }            return new MvcHtmlString("");        }    }}

 

 

Register the custom DataAnnotationsModelMetadataProvider globally.

Protected void Application_Start ()
{
......

ModelMetadataProviders. Current = new CustomModelMetadataProvider ();
}

 

View:

@ Model MvcApplication1.Models. User
@ Html. TextBoxFor (model => model. Name, new {title = @ Html. TooltipFor (model => model. Name )})

 

References:
Creating your own modelmetadataprovider to handle custom attributes

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.