I have a couple of MVC4 projects, and I'd like to make a set of display / editor templates that are reused across these projects. I'd like to create a common project that contains these templates, and reference it in the other projects.
So I have the views defined in my common project, along with some other utilities:
Common
- Models
- CommonMetadataAttribute.cs
- Views
- Shared
- EditorTemplates
- Decimal.cshtml
- Object.cshtml
- Password.cshtml
- String.cshtml
- Switch.cshtml
- ViewTemplates
- Object.cshtml
- Switch.cshtml
- Error.cstml
And in my client project's models I'd have
using MobileWeb.Common
public class MyViewModel
{
[UIHint("Switch")]
[CommonMetadata(Theme = "foo")]
public bool Enable { get; set; }
}
And in the client projects views I'd be able to generate my custom editors by simply calling:
@model MyViewModel
...
@Html.EditorFor(model => model.Enable)
Ideally, the client projects could override with their own templates, and it would just fall back to the common templates if no project-specific one was defined.
I've considered MvcContrib's portable areas, but I'm not sure if it will work since this is not really an area and I don't really know how to go about setting it up. Before I go down that road I'd like to know what is the preferred way of doing this?
Personally I have always been packaging templates as custom NuGets. You put your custom templates in a NuGet and when the user installs this NuGet in his project they are automatically added to the correct folders of his application. And automagically all the EditorFor/DisplayFor calls throughout the application start using your custom templates instead of the default ones.
This way you don't need to be writing some custom VirtualPathProviders (which break as soon as you precompile your application) in order to be retrieving Razor views embedded in third party assemblies and so on... Another advantage of using NuGets is that the user has your templates in his application and could easily modify them if he is not pleased with your work. And if he is really angry with you, all that he needs is to uninstall your NuGet in order to rollback his application to the default state.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With