MVC editor templates in different assembly

791 views Asked by At

I'd like to create a common assembly that I can use in several MVC projects (MVC 4 preferred). This assembly has to provide editor templates for primitive types. For example, a date picker. How can I do this?

I know of two ways to achieve similar goals, but I don't like those. One way is to forget about MVC's templating system and provide custom HtmlHelper extensions, like @Html.MyOwnDatePickerFor(x=>x.Field). I know of some UI frameworks that do this. I don't like it because it is not transparent (@Html.EditorFor() won't pick it up automatically) and it feels weird to have an editor template system in MVC and not use it. The other way is the actual MVC template system, by creating partial views named like EditorTemplates/datatype.cshtml (or .ascx), but it doesn't work because they have to be in the MVC project, I don't know how I can share these between multiple projects.

Any suggestions?

2

There are 2 answers

1
Tom Styles On BEST ANSWER

We have a few classes that provide the equivalent of editorfor but surrounded by different HTML. We have one for @Html.CMSEditorFor(m => m.Name) that uses the same HTML as our content management system and one for @Html.BootstrapEditorFor(m => m.Name) that churns out HTML in a nice Bootstrap compatible way.

I can't claim any credit for the idea though it comes directly from here. ASP.NET MVC 3 Custom HTML Helpers- Best Practices/Uses

2
Chris Pratt On

You're looking for RazorGenerator. It's made by the Razor team to allow you to compile Razor views just for this purpose: reusing the views by including the assembly in other projects.