2008-10-16 23 views
198

如何讓視圖渲染來自不同文件夾的部分(用戶控件)? 預覽3我曾經用完整的路徑調用RenderUserControl,但是如果升級到預覽5,這是不可能的。 取而代之,我們得到了RenderPartial方法,但它不提供我正在尋找的功能。從不同文件夾渲染部分(不共享)

回答

370

只包含視圖的路徑和文件擴展名。

剃刀:

@Html.Partial("~/Views/AnotherFolder/Messages.cshtml", ViewData.Model.Successes) 

ASP.NET引擎:

<% Html.RenderPartial("~/Views/AnotherFolder/Messages.ascx", ViewData.Model.Successes); %> 

如果這不是你的問題,你可以請註明您的代碼,用於與RenderUserControl工作?

+13

我希望我們可以說/ AnotherFolder /消息 – 2009-03-13 03:10:50

+4

@Simon_Weaver你可以做到這一點。一種方法是修改ViewEngine並使用類似於if(partialViewName.Contains「/」)partialViewName =「〜/ Views /」+ partialViewName;` – 2011-01-30 17:31:32

+2

在MVC 3 Razor引擎中工作來覆蓋它的`FindPartialView`方法,但像上面那樣,你需要擴展名(.cshtml)。 – Chris 2012-03-03 07:26:24

3

WebFormsViewEngine基於的VirtualPathProviderViewEngine應該支持路徑前面的「〜」和「/」字符,因此您的示例應該可以工作。

我注意到你的例子使用路徑「〜/ Account/myPartial.ascx」,但你提到你的用戶控件位於Views/Account文件夾中。您是否嘗試過

<%Html.RenderPartial("~/Views/Account/myPartial.ascx");%>

或者是剛剛在你的問題一個錯字?

6

對於命名myPartial.ascx的用戶控件位於瀏覽/文件夾的帳戶像這樣寫:

<%Html.RenderPartial("~/Views/Account/myPartial.ascx");%> 
0

你應該代碼之前嘗試這個

~/Views/Shared/parts/UMFview.ascx 

地方~/Views/

-4

嘗試使用RenderAction("myPartial","Account");

3

我已創建d一種似乎工作得很好的解決方法。我發現需要切換到不同的控制器,用於動作名稱查找,查找視圖,等等。爲了實現此的上下文中,創建了一個新的擴展方法用於HtmlHelper

public static IDisposable ControllerContextRegion(
    this HtmlHelper html, 
    string controllerName) 
{ 
    return new ControllerContextRegion(html.ViewContext.RouteData, controllerName); 
} 

ControllerContextRegion定義爲:

internal class ControllerContextRegion : IDisposable 
{ 
    private readonly RouteData routeData; 
    private readonly string previousControllerName; 

    public ControllerContextRegion(RouteData routeData, string controllerName) 
    { 
     this.routeData = routeData; 
     this.previousControllerName = routeData.GetRequiredString("controller"); 
     this.SetControllerName(controllerName); 
    } 

    public void Dispose() 
    { 
     this.SetControllerName(this.previousControllerName); 
    } 

    private void SetControllerName(string controllerName) 
    { 
     this.routeData.Values["controller"] = controllerName; 
    } 
} 

這是一個視圖中使用的方法如下:

@using (Html.ControllerContextRegion("Foo")) { 
    // Html.Action, Html.Partial, etc. now looks things up as though 
    // FooController was our controller. 
} 

可能有不必要的副作用這一點,如果你的代碼需要controller路由組件不會改變,但在我們的代碼中,目前似乎沒有任何負面影響。

18

在我的情況下,我使用MvcMailer(https://github.com/smsohan/MvcMailer)並希望從另一個文件夾中訪問不在「共享」中的部分視圖。上述解決方案不起作用,但使用相對路徑。

@Html.Partial("../MyViewFolder/Partials/_PartialView", Model.MyObject) 
18

如果您正在使用這個其他路徑,很多時候您可以永久修復此問題,而無需一直指定路徑。默認情況下,它將檢查View文件夾和Shared文件夾中的部分視圖。但是說你想添加一個。在你的Global.asax.cs文件然後

public class NewViewEngine : RazorViewEngine { 

    private static readonly string[] NEW_PARTIAL_VIEW_FORMATS = new[] { 
     "~/Views/Foo/{0}.cshtml", 
     "~/Views/Shared/Bar/{0}.cshtml" 
    }; 

    public NewViewEngine() { 
     // Keep existing locations in sync 
     base.PartialViewLocationFormats = base.PartialViewLocationFormats.Union(NEW_PARTIAL_VIEW_FORMATS).ToArray(); 
    } 
} 

,添加以下行:

類添加到您的模型文件夾

ViewEngines.Engines.Add(new NewViewEngine());