6

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?

4

1 回答 1

1

老实说,我没有尝试查看 AdditionalValues 是否被正确提取,但是在您看来,内置为您提供了什么?

    @using System.Web.Mvc

    @{
         var meta = ModelMetadata.FromLambdaExpression(model => model, ViewData);
     }
于 2013-12-09T23:19:16.540 回答