5

I want to use the MetaDatatType attribute in a MVC6 project and it does't seem to be available yet. I am using 7.0.0-rc1-final so it should the latest.

Does anyone know if it is in another assembly? I get the error, The type or namespace name MetadataType could not be found.

2 Answers 2

2

After some googling it seems like this attribute is not available in MVC 6. You can use it in the full DNX, but it is unavailable in DNX Core.

Of course, you can decorate your code with

#if DNXCORE50
...
#endif

so that you get no errors when compiling for the full DNX, but it looks like the attribute does not do what it is expected. For example, DisplayName(Name = "...") does not work (I didn't check the other options like Required etc.).

IMHO, Fluent MetadataProvider may be a solution to this, only I do not know if it was ported to vNext. I am going to contact the author(s) and if they are not able to port it soon I'll fork it and will make an attempt to port it myself.

EDIT: ModelMetadataType replaces MetadataType. You must reference Microsoft.AspNet.Mvc.Core. I am not quite sure but maybe you'd need Microsoft.AspNet.Mvc.DataAnnotations too.

Anyway, my thoughts about Fluent Data Annotations (Fluent MetadataProvider) are still valid. You may also need to read an interesting article about this: Why You Don't Need ModelMetadata.Attributes by Brad Wilson.

4
  • How does ModelMetaDataType replace MetaDataType? One is specific to MVC one is Not? They aren't even in the same namespace. Mar 18, 2016 at 16:23
  • @Erik Philips: You are right - MetadataTypeAttribute is in System.ComponentModel.DataAnnotations, while ModelMetadataTypeAttribute is in Microsoft.AspNet.Mvc. So, what? We are talking about different attributes having the same impact. Please, read the question carefully. And, yes, the second one replaces the first, regarding metadata description. Try what I have described and you'll see the difference. Mar 19, 2016 at 5:50
  • I think it depends on usage. Is he using it for a Metadata Provider or for Validation. Mar 19, 2016 at 5:59
  • Whatever the usage is MetadataTypeAttribute (comming from DataAnnotations) is not available for DNX Core. Thus the errors. Mar 21, 2016 at 8:03
1

I've ran into same problem. I'm experienced with Asp.NET MVC 5 but I feel a newbie with Asp.Net Core 1.0 (Full Framework 4.6.1). I have a Class Library for models (Framework 4.6.1) using telerik dataaccess core. Using MetadataTypeAttribute for decorating a model class by using a buddy class was almost a headache due to it not worked at all!. Trying almost anything was frustrating. So what was the solution for me?

I had to use ModelMetadataTypeAttribute instead of MetadataTypeAttribute from Microsoft.AspNetCore.Mvc.Core assembly. Decorating a metadata class (buddy class) follows same methodology of using Display(...), DisplayName(...), same for validators. No matter if the buddy class (Metadata Class) is located external from or internal to the model class.

However using MetadataTypeAttribute directly with the model class instead of a buddy class, works perfectly!

The only explanation I could give about this divergence is related with the new emerging Microsoft Asp.Net core technology, relocation of DLL process and functionalities.

PD: It's not required Microsoft.AspNetCore.Mvc.DataAnnotations

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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