Tevin
Tevin

Reputation: 1414

Sharing ASP.NET MVC partial views between projects

What is the best way to share a common Partial View between applications? I've created a separate assembly containing my Partial View in an ascx file, some scripts that go with that view and an HtmlHelper extension method to make creating the partial view easier. However, when referencing that assembly from an ASP.NET MVC application, it can't find the partial view as the ascx file is not copied as well. Also, what is the best way of including the attached scripts with the application? The only way I can see at the moment is to copy the relevant files to the new application.

Upvotes: 22

Views: 3429

Answers (4)

Chris Haines
Chris Haines

Reputation: 6555

Our approach with WebForms has been to create an IIS virtual directory for shared user controls. Does this approach work with MVC?

Upvotes: 0

orip
orip

Reputation: 75557

This post discusses how to embed a view in a DLL and render it from a different project. I guess it'll work for partials as well, but I haven't tried it.

Upvotes: 4

griegs
griegs

Reputation: 22770

This is an ongoing issue even with WebForms. Sharing this stuff is not easy.

My prefered approach now is to create my controls either as mvc WebControls or as jQuery plugins. That way I can pass around a versioned DLL or script file.

I lean heavily towards the WebControls solutions because, like I said, I can version it and put it in the company infrastructure framework.

I then (only) use PartialViews to render the webcontrols and add some basic flair, markup or content.

Upvotes: 2

user151323
user151323

Reputation:

A nice question indeed.

I would suggest avoiding ascx files and generating HTML manually in your HTML helpers. TagBuilder class does a great deal of help here.

Upvotes: 7

Related Questions