1

I want to create a Model:

public class TestModel
{
    Microdata(Data = "data-test=this is a test!")]
    public bool Test { get; set; }
}

Then in the view:

@Html.DisplayForModel()

The result i'm looking for is something like this:

<label>Test:</label> <input type="checkbox" data-test="this is a test!" />

I've already created a custom attribute class, but this didn't produce anything.

[AttributeUsage(AttributeTargets.Property | AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
public class MicrodataAttribute : Attribute
{
    public string Data { get; set; }

    public RouteValueDictionary GetAttributes()
    {
        var attributes = new RouteValueDictionary();

        if (this.Data != null)
        {
            string[] kv = this.Data.Split(',');
            attributes.Add(kv[0], kv[1]);
        }
        return attributes;
    }
}



public class MetadataProvider : DataAnnotationsModelMetadataProvider
{
    protected override ModelMetadata CreateMetadata(IEnumerable<Attribute> attributes, Type containerType, Func<object> modelAccessor, Type modelType, string propertyName)
    {
        var metadata = base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName);
        var additionalValues = attributes.OfType<HtmlPropertiesAttribute>().FirstOrDefault();
        if (additionalValues != null)
        {
            metadata.AdditionalValues.Add("HtmlAttributes", additionalValues);
        }
        return metadata;
    }
}

2 Answers 2

2

Why would it? There's no code that uses your attribute...

Read the blog post below - it describes how MVC uses metadata and has an example of a custom object template you will need to write:

http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-1-introduction.html

Sign up to request clarification or add additional context in comments.

10 Comments

I don't understand what you mean when you say there is no code that uses my attribute?? The whole point to using Models in MVC is to model your db or view or whatever for your application. These Models are going to be created dynamically and view just the same. Also, I need to use custom HTML5 data dash attributes.
@JasonFoglia - No code in MVC framework is using your HtmlProperties attribute.
How is this possible when there is data validation using html5 data dash attributes? ex: data-val Is there no way to manipulate data validation? As you already know there is somewhat of a way to do this. You sent me a link, here's another ruijarimba.wordpress.com/2011/07/24/…
@JasonFoglia - Sorry, I don't get what your problem is. Look at your question. You've posted a source for your custom attribute and a call to Html.DisplayForModel. You've stated that your custom attribute didn't produce anything. I've answered that this is because MVC knows nothing about your custom attribute and therefore will not use it in any way. The solution is to create custom metadata provider that will analyze your custom attribute (like in the url you've posted above). I assume you haven't created custom provider since you didn't mention it. What is that you don't understand?
I have created a custom metadata provider and I just posted it.
|
0

After discussing the issue with @JakubKonecki and reading the blog post he presented. Here is my EditTemplate used to help create data dash attributes in MVC 2/3 and most likely 4.

I saved this file under root/Views/Shared/EditTemplates as String.cshtml. cshtml since i'm using the razor engine. The location can be different if you are using Area's and they don't have to be stored in "Shared" views folder. Just read the entire blog @JakubKonecki posted by Brad Wilson.

Thanks again @JakubKonecki!

@{
    Dictionary<string, object> AV = ViewData.ModelMetadata.AdditionalValues;
    Dictionary<string, object> htmlAttr = new Dictionary<string,object>();
    foreach (KeyValuePair<string, object> A in AV)
    {
        if (A.Value is System.Web.Routing.RouteValueDictionary)
        {
            foreach (KeyValuePair<string, object> B in (System.Web.Routing.RouteValueDictionary)A.Value)
            {
                htmlAttr.Add(B.Key, B.Value);
            }
        }
    }
    htmlAttr.Add("class", "text-box single-line");
    htmlAttr.Add("type", "text");
}

@Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue, htmlAttr)

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.