Reputation: 843
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.
Upvotes: 5
Views: 763
Reputation: 2421
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
Upvotes: 1
Reputation: 10045
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.
Upvotes: 2