2

I want to make additional editor template for the Int32. In it I want to get all attributes(custom/default-data-annotations) and do some work with them:

@model Int32

@{
    string propertyName = ViewData.ModelMetadata.PropertyName;
    var attributes = ViewData.ModelMetadata.GetSomeHowAllTheAttributesOfTheProperty;
    SomeWorkWithAttribute(attributes);

}
<input type="text" name="@propertyName" value="@ViewData.ModelMetadata.DisplayFormatString" class="form-control"/>

So the question how to get in the EditorTemplate all attributes of the property?

Thx in advance

Maris
  • 4,608
  • 6
  • 39
  • 68

2 Answers2

4

EDIT: initial answer removed (not enough info in the question):

Since you need to access your custom attribute, I suggest the following:

A: if you're just saving some values using your custom attribute, use the AdditionalMetadataAttribute:

Model:

[Additional("myAdditionalInfoField", "myAdditionalInfoValue")]
public int IntProperty { get; set; }

View:

@{
    string valueFromAttribute = ViewData.ModelMetadata.Additional["myAdditionalInfoField"].ToString();
}

B: if you're doing more complex work inside your custom attribute, get the current metadata property for your model and save the computed values inside the ViewData.ModelMetadata.Additional dictionary. Please note that this requires that you implement your custom metadata provider. A detailed implementation can be found here.

Gyrfalcon
  • 105
  • 7
Andrei V
  • 7,306
  • 6
  • 44
  • 64
  • The question is - "How to get all attributes of the property?" – Maris Jan 27 '14 at 12:55
  • Sorry, I didn't understand you completely. "All attributes is a bit generic". MVC "saves" some of the values in the metadata properties. For example, the `Required` attribute is saved in `ModelMetadata.IsRequired` and so forth. Can you be more specific? Another problem is that you're actually targeting a model which is a value type and not a reference type. If it's not a property of a class, you are not able to set attributes. If it's a class property, you need to provide the class instance so that you can retrieve all "explicit" attributes using reflection. – Andrei V Jan 27 '14 at 13:04
  • I Have the Attribute - `MySomeCustomAttribute` I want to be sure that the property have/not have that attribute... – Maris Jan 28 '14 at 10:59
  • @Maris, I don't know if that's possible. You could get attribute using reflection but you would need the type of the containing class. Another option would be to use the `AdditionalMetadata` attribute and save your values there. It's much easier and you can access it trough `ViewData.ModelMetadata.AdditionalValues`. – Andrei V Jan 28 '14 at 11:06
  • I know about `reflection` but hoped that it is arriving somewhere in ViewData... Maybe someone have any idea about it. – Maris Jan 28 '14 at 11:18
  • As I said, you can use the `AdditionalMetadata` attribute. It's simple and available in your view. – Andrei V Jan 28 '14 at 11:20
  • @Maris, please take a look at my edits. They might prove useful. – Andrei V Jan 28 '14 at 11:31
  • That is exectly what I was looking for.Thank you! – Maris Jan 29 '14 at 10:04
4

this is how to access all the attributes in your model property from the DisplayTemplate or EditorTemplate:

var attributes = ((Microsoft.AspNetCore.Mvc.ModelBinding.Metadata.DefaultModelMetadata)ViewData.ModelMetadata).Attributes;

then you can get the desired custom attribute:

var yourAttribute = attributes.PropertyAttributes.OfType<YourAttribute>().FirstOrDefault();

now you have yourAttribute to do what ever you want!

Muhammad Assar
  • 659
  • 7
  • 16