Ok, the last blog on Model Level Resource Declaration allows you to set the resource at a model level, but what if your models are not in your UI layer and your resources are?
How about registering the type of resource to use for a particular model type, model type hierarchy or even globally, for example ...
protected void Application_Start() {
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
DependencyResolver.SetResolver(
t => {
if (t == typeof (ModelMetadataProvider)) {
return new DisplayResourceMetadataProvider();
}
return null;
},
t => { return new object[] {}; });
// Use the following resource for all types
DisplayResourceMetadataProvider
.UseForProperty<GlobalResource>();
// Use the following resource for Person and Derived
DisplayResourceMetadataProvider
.UseForProperty<PersonResource>(typeof(PersonModel));
}The DisplayResourceMetadataProvider overrides the ModelMetadata.DisplayName which is then shown when using Html.LabelFor in your view.
To demonstrate the hierarchical retrieval there are two types in the application
public class PersonInfoModel {
public string Name { get; set; }
public string Email { get; set; }
public string MobileNumber { get; set; }
}
public class PersonModel : PersonInfoModel {
public string Biography { get; set; }
}With the following Resources


Running the attached application you are presented with a link to two forms..
With the code as above, where PersonResource is assigned to PersonModel, the Person Info form will not find any resources in PersonResource and will fall back to those found in GlobalResource..

If you change the registration to use PersonInfoModel instead of PersonModel..
// Use the following resource for Person and Derived
DisplayResourceMetadataProvider
.UseForProperty<PersonResource>(typeof(PersonInfoModel));then the form will render with resources found in PersonResource.. fab

Sandbox.DisplayResources.zip ASP.NET MVC 3 VS2010 solution
| < | May 2012 | |||||
|---|---|---|---|---|---|---|
| S | M | T | W | T | F | S |
| 29 | 30 | 1 | 2 | 3 | 4 | 5 |
| 6 | 7 | 8 | 9 | 10 | 11 | 12 |
| 13 | 14 | 15 | 16 | 17 | 18 | 19 |
| 20 | 21 | 22 | 23 | 24 | 25 | 26 |
| 27 | 28 | 29 | 30 | 31 | 1 | 2 |
Add-ins AJAX ASP.NET MVC Browsers C# Caching CodeDom Compression CORS CSS CV Data Database DependencyResolver Development Dynamic Entity Framework Error Handling Extend File Upload Forms GDI+ HTML HTML Editor HTTP Interfaces JavaScript JQuery MCE MetadataProvider MSBuild Numbers Objects Patterns Progressive Enhancement Projects Publish Regex Resources Security SEO SMTP Source Control Strings Sub-Collections TDD Templates Tools Twitter User Interface WCF Web Development WHS WMC XLinq XML
15/05/2012
WindowsAzure
Announcing the MEET Windows Azure Event! Streamed online June 7th. Register at http://t.co/bObzTAuL #MEETAzure #WindowsAzure
10/05/2012
kevinwhinnery
Comparing Titanium and PhoneGap - A common question I get asked at developer events and conferences is how... http://t.co/Zq2eND6B
09/05/2012
brianleroux
PhoneGap goals and philosophy: http://t.co/wkq8wI2T
just now
mstechfeed
#MSDN Virtual Lab: #Windows Azure: Running a Parametric Sweep Applications with the Windows Azure HPC Scheduler
http://t.co/3676GXSQ
just now
ilcomm
#android #jobs encrypting phonegap android by joseesfera http://t.co/s4VgtPhs #israel #iphone