Questions tagged [modelmetadataprovider]

ASP.NET MVC 3 Model Metadata Providers

ASP.NET MVC 3 introduced the support for new DataAnnotation DisplayAttribute. ModelMetadataProvider can be used to create ModelMetadata based on the model’s attributes. Create a MetadataProvider to support any custom behavior.

Important Links

24 questions
16
votes
3 answers

Technique for carrying metadata to View Models with AutoMapper

I use AutoMapper to map my domain objects to my view models. I have metadata in my domain layer, that I would like to carry over to the view layer and into ModelMetadata. (This metadata is not UI logic, but provides necessary information to my…
5
votes
2 answers

Get ModelMetadata for a class in .NET Core

In Entity Framework 6 I could get the ModelMetadata for a class (myModel) like this: var modelMetadata = ModelMetadataProviders.Current.GetMetadataForType(null, myModel.GetType()); How can I do the same in .net core 1.1.1?
5
votes
1 answer

How to Transfer DataAnnotation metadata to ViewModel with AutoMapper with Betty's Approach

I need clarification on how to implement Betty's code solution to transferring data annotation metadata to ViewModels with AutoMapper (see here). Or if you have a better way, please share that. Maybe the implementation of Betty's answer is obvious…
4
votes
1 answer

Retrieve model name in custom display name attribute

Here is my development requirement, My label values are stored in the database, and I still want to use the data annotation in a declarative way, this is to make my model more readable. And here is my approach, I decided to write custom…
4
votes
0 answers

Custom model metadata provider caching issue

In order to allow us dynamic control over labels and error messages, we created a custom DataAnnotationsModelMetadataProvider. In a Display attribute we store the key in the Name property and using the custom DataAnnotationsModelMetadataProvider we…
Elad Lachmi
  • 10,406
  • 13
  • 71
  • 133
4
votes
1 answer

Add new Property to MVC ModelMetadata

I'm seeing alternative approaches to this such as AdditionalValues, but I'm wondering if it's possible to end up in a scenario where you can add a new property to the ModelMetadata object available in template views. For example you could have:…
Jacques
  • 6,936
  • 8
  • 43
  • 102
3
votes
1 answer

ModelMetadata TemplateHint always null

I am trying to create a custom ModelMetadataProvider to provide unobtrusive attributes for the JQuery UI Autocomplete widget. I have a custom attribute that looks like this: [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)] public…
3
votes
1 answer

ASP.NET MVC - Model's metadata used from model object type instead of declared model of the View

I have interface of a model declared, with class implementing it: public interface IMyModel { [Range(1, 1000)] [Display(Name = "ModelProp From Interface")] int MyIntProperty { get; set; } IMySubModel SubModel { get; } } public…
2
votes
0 answers

Tests involving TryValidateModel fail when run with test using custom HtmlHelper

I have a test of an HtmlHelper extension method that works great when it is the only test that I run. It appears below. All of my other unit tests succeed when I run them, as long as I don't run this test at the same time. But when I run this and…
MKP
  • 65
  • 5
2
votes
1 answer

ASP.NET MVC : access container from EditorTemplate

I have a view model AssetFreezeViewModel.cs that contains the following property : public AssetFreezeAccountBalanceViewModel AccountBalanceInfo { get; set; } I have a view Details.cshtml based on the ViewModel above with that line…
2
votes
1 answer

ModelMetadata.Container property is null

I am writing a custom ModelMetadataProvider which extends default DataAnnotationsModelMetadataProvider. Unfortunately i came across an issue: ModelMetadata.Container property is always null. According to msdn description of ModelMetadata Class,…
Alex Art.
  • 8,711
  • 3
  • 29
  • 47
2
votes
1 answer

ASP.NET MVC 5 / Metadata services - DataAnnotations - is this still the preferred way?

I haven't followed the ASP.NET MVC evolution since version 3 or so. Back then in MVC3 the simplest/preferred way to boost entity metadata info for the MVC DisplayNameFor etc magic was to use DataAnnotations. With the possible use of the…
Peter Aron Zentai
  • 11,482
  • 5
  • 41
  • 71
2
votes
1 answer

Do model metadata providers in ASP.NET MVC 3 support dynamic metadata?

I'm trying to implement some custom model metadata in ASP.NET MVC 3. I can't use data annotation attributes since some of the metadata could change based on configured values, so I need a solution that will let me set the metadata on every request.…
1
vote
0 answers

Customize ModelMetadata with CreateMetadata

The first time a user chooses a value from a select list, we show a default option with the text Pick one and value -1. To help with this we have a custom attribute NotEqualAttribute. When a drop-down list is required we have to use both attributes…
Razcer
  • 394
  • 4
  • 17
1
vote
2 answers

Using IoC to provide a custom ModelMetadataProvider in MVC3

I'm currently overriding the default ModelMetadataProvider in the Global.asax file using this ModelMetadataProviders.Current = new RedSandMetadataProvider(ModelMetadataProviders.Current); and this works perfectly. But I'd like to use the…
Nick Albrecht
  • 16,607
  • 10
  • 66
  • 101
1
2