Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

If I have a convention to change the editor and set some values

public class MetadataProvider : DataAnnotationsModelMetadataProvider
{
    protected override ModelMetadata GetMetadataForProperty(Func<object> modelAccessor, Type containerType, System.ComponentModel.PropertyDescriptor propertyDescriptor)
    {
        var meta = base.GetMetadataForProperty(modelAccessor, containerType, propertyDescriptor);
        if (IsNumericType(propertyDescriptor.PropertyType))
        {
            meta.TemplateHint = "Number";

            var attr = propertyDescriptor.Attributes.OfType<RangeAttribute>().FirstOrDefault();
            if (attr != null)
            {
                meta.AdditionalValues["min"] = attr.Minimum;
                meta.AdditionalValues["max"] = attr.Maximum;
            }
        }
        return meta;
    }
    //...
}

Then I can get the additional values in the template

@{
    var min = ViewData.ModelMetadata.AdditionalValues["min"];
    var max = ViewData.ModelMetadata.AdditionalValues["max"];
}

However, if I use the same template like this

@Html.EditorFor(x => x.Number, new { min = 1, max = 10 })

Then I should get the values like this

@{
    var min = ViewData["min"];
    var max = ViewData["max"];
}

Can I somehow merge additionalViewData and ModelMetadata.AdditionalValues so that I could get the values from one place?

share|improve this question
    
Are you saying you are unable to get the values from ViewData within your Template view? –  Ben Foster Jul 17 '13 at 8:40
    
I don't understand your question. ViewData and ModelMetadata.AdditionalValues are two different things. ViewData is a property of Controller and ViewPage. AdditionalValues is a property of ModelMetadata. Of course, they can't be merged. If you wanna show the min and max values of your Range Attribute, I recommend creating an Html Helper. –  ataravati Jul 27 '13 at 4:25

1 Answer 1

I honestly haven't tried to see if the AdditionalValues get pulled properly but what does the built-in provide for you in your view?

    @using System.Web.Mvc

    @{
         var meta = ModelMetadata.FromLambdaExpression(model => model, ViewData);
     }
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.