Question

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?

Was it helpful?

Solution

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

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top