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

前端 未结 2 820
情深已故
情深已故 2021-01-17 13:12

I have two MVC projects one as a parent project and the other as a child project. The child project adds reference to the parent project. I like to use partial views from th

相关标签:
2条回答
  • 2021-01-17 14:03

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

    Something like this:

    @Html.Partial("~/View/Shared/_GenericGreeting.cshtml")
    
    0 讨论(0)
  • 2021-01-17 14:16

    I accepted teo's answer. He helped me and gave me clue to the problem. But there are few important things to check.

    One thing I wasn't aware is the generated cshtml files have properties called PageVirtualPathAttribute. One problem i couldn't get the right combination is because i was getting the path wrong.

    Another thing to look is this class which is auto generated when you installed RazorGenerator and it is in your 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 define path to your compiled view. My shared project was created by someone else and he put this path in it.

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

    Once I have those bits checked, I just need to use like the following in my view as teo suggested.

    @Html.Partial("~/Areas/Cart/Views/Home/_GenericGreeting.cshtml")
    
    0 讨论(0)
提交回复
热议问题