Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Data Annotations ASP.NET MVC C#

I have the follwing question about MVC 2 with C#.

Here is my Model:

public class Pmjob
{
   [Tooltext="Hier soll der Name eingegeben werden"]
   [DisplayName("Type")]
   public int Name { get; set; }
}

Now I want to reach the Tooltext item in my view, e. g.:

@Html.ToolTextFor(Model => Model.Pmjob.Name)

or in the BL:

if ( Model.Pmjob.Name.Tooltext == "") {
}

Is this possible?

like image 650
Stefan aus Linz oida Avatar asked Apr 05 '11 06:04

Stefan aus Linz oida


People also ask

Can we create custom data annotations?

The MVC framework has great extensibility features and because of this we can create our own customized data annotation attributes.

What is data annotation in MVC?

Advertisements. DataAnnotations is used to configure your model classes, which will highlight the most commonly needed configurations. DataAnnotations are also understood by a number of . NET applications, such as ASP.NET MVC, which allows these applications to leverage the same annotations for client-side validations.


1 Answers

Create an abstract class MetaDataAttribute :

public abstract class MetadataAttribute : Attribute
{
    /// <summary>
    /// Method for processing custom attribute data.
    /// </summary>
    /// <param name="modelMetaData">A ModelMetaData instance.</param>
    public abstract void Process(ModelMetadata modelMetaData);
}

Make your attribute inherit from MetaDataAttribute :

public class ToolTextAttribute : MetadataAttribute
{
    public string Text { get; set; }
    public TooltextAttribute(string text)
    {
        this.Text = new text;
    }

    public override void Process(ModelMetadata modelMetaData)
    {
        modelMetaData.AdditionalValues.Add("ToolText", this.Text);
    }
}

Create the custom MetaDataProvider :

public class CustomModelMetadataProvider : DataAnnotationsModelMetadataProvider
{
    protected override ModelMetadata CreateMetadata(
        IEnumerable<Attribute> attributes,
        Type containerType,
        Func<object> modelAccessor,
        Type modelType,
        string propertyName)
    {
        var modelMetadata = base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName);
        attributes.OfType<MetadataAttribute>().ToList().ForEach(x => x.Process(modelMetadata));
        return modelMetadata;
    }
}

And replace the default one (global.asax.cs) :

protected void Application_Start()
{
    // snipped

    ModelMetadataProviders.Current = new CustomModelMetadataProvider();
}

Finally, you can access it in your view (or in a Html Helper ) :

(string)ViewData.ModelMetadata.AdditionalValues.Where(x => x.Key == "ToolText").SingleOrDefault()

Source :

  • http://weblogs.asp.net/seanmcalinden/archive/2010/06/11/custom-asp-net-mvc-2-modelmetadataprovider-for-using-custom-view-model-attributes.aspx
  • http://weblogs.asp.net/seanmcalinden/archive/2010/06/12/asp-net-mvc-2-auto-complete-textbox-custom-view-model-attribute-amp-editortemplate.aspx
like image 183
mathieu Avatar answered Oct 03 '22 15:10

mathieu