How to use partial view from another project in asp.net mvc

I have two MVC projects, one of which is a parent and the other a child. A link to the parent project is added in the child project. I like to use partial views from the parent project from the child project, something like -

@Html.Partial("_GenericGreeting") <- in a child project

_GenericGreeting.cshtml <- located in the source project

A children's project is a project that is being launched. The parent project, which I mean, is similar to the base / general project. RazorGenerator was installed in both projects, and each project can be compiled into separate assemblies.

When I run the project, I get the following error.

Partial view '_GenericGreeting' not found or not viewer supports found locations.

If I copy a partial view and paste it into a child project, thatโ€™s fine, but I donโ€™t want to duplicate the files.

I tried this post but no luck, but maybe I am not adding it correctly.

+4
source share
2 answers

Not a very good, but simple solution that can solve your problem. One of the overloads @Html.Partial() allows you to write the full path to your view.

Something like that:

 @Html.Partial("~/View/Shared/_GenericGreeting.cshtml") 
+1
source

I accepted teo answer. He helped me and made me understand the problem. But there are a few important things to check.

One thing that I did not know about was that the generated cshtml files have properties called PageVirtualPathAttribute . One problem, I could not get the right combination, because I misunderstood the way.

Another thing to look at is the class that is automatically created when RazorGenerator is installed and is located in the App_Start folder.

 public static class RazorGeneratorMvcStart { public static void Start() { var engine = new PrecompiledMvcEngine(typeof(RazorGeneratorMvcStart).Assembly) { UsePhysicalViewsIfNewer = HttpContext.Current.Request.IsLocal }; ViewEngines.Engines.Insert(0, engine); // StartPage lookups are done by WebPages. VirtualPathFactoryManager.RegisterVirtualPathFactory(engine); } } 

However, this line has an overload method to determine the path to your compiled view. My common project was created by someone else, and he introduced this path into it.

 var engine = new PrecompiledMvcEngine(typeof(RazorGeneratorMvcStart).Assembly, @"~/Areas/Cart/", null) 

Once I checked these bits, I just need to use, as shown below, in my view as suggested.

 @Html.Partial("~/Areas/Cart/Views/Home/_GenericGreeting.cshtml") 
+1
source

All Articles