Is it possible to access an additional custom model property from viewdata.modelmetadata.properties?

1.1k views Asked by At

On a specific View, I pass in a new model to a partial view via:

@Html.Partial("view.cshtml", new Model1())

Then on the partial I need to access all of its properties, and attributes, including a new custom attribute I made.

My new attribute uses MetadataAware:

public class ReportDescriptionAttribute : Attribute, IMetadataAware
{
    public ReportDescriptionAttribute(string resourceKey)
    {
        this.ReportDescription = resourceKey;
    }

    public string ReportDescription { get; set; }

    public void OnMetadataCreated(ModelMetadata metadata)
    {
        metadata.AdditionalValues["ReportDescription"] = this.ReportDescription;
    }
}

so on my model I can now use [ReportDescription("resourcekey")]

But in my partial view, when I use @foreach (var property in ViewData.ModelMetadata.Properties) it does not show up.

*I also created the HTML helper class, as stated in this very similar question, but cannot get it to work.

Thoughts? Thanks for your help

1

There are 1 answers

1
password On BEST ANSWER

Above, Stephen was able to point me in the right direction. My additional attribute was there the whole time, and I didn't even need the helper class, just the IMetaDataAware.

@foreach (var property in ViewData.ModelMetadata.Properties)
{
    <div style="margin:10px">
        <strong>@(property.DisplayName)</strong>
        <br />
        @foreach (var addProp in property.AdditionalValues)
        {
            if (addProp.Key == "ReportDescription")
            {
                <span>@addProp.Value</span>
            }

        }
    </div>
}