2010-02-04 49 views
0

如何將Html.RenderPartial用於其他文件夾中的PartialViews?對其他視圖/文件夾中的文件使用Html.RenderPartial

我曾嘗試爲:

<% Html.RenderPartial("~/Views/User/Users.ascx", Model); %> 

它拋出一個錯誤:

System.InvalidOperationException: The partial view '~/Views/User/Users.ascx' was not found. The following locations were searched: 
    ~/Views/Main/~/Views/User/Users.ascx.aspx 
    ~/Views/Main/~/Views/User/Users.ascx.ascx 
    ~/Views/Shared/~/Views/User/Users.ascx.aspx 
    ~/Views/Shared/~/Views/User/Users.ascx.ascx 

是什麼,在這裏失蹤或其無法調用partialview在其他文件夾?

回答

0

如果要更改查找偏好,查看或主頁的規則,必須更改ViewEngine。

public class ChangedWebFormViewEngine : WebFormViewEngine 
{ 

     private static string[] LocalViewFormats = 

     new string[] { 
      "~/Views/{1}/OtherPath/{0}.aspx", 
     "~/Views/{1}/{0}.aspx", 
     "~/Views/{1}/{0}.ascx", 
     "~/Views/Shared/{0}.aspx", 
     "~/Views/Shared/{0}.ascx" 
    }; 

     public LocalizationWebFormViewEngine() 
     {  
     base.ViewLocationFormats = LocalViewFormats; 
     base.PartialViewLocationFormats = LocalViewFormats; 
     base.MasterLocationFormats = new string[] { 

       "~/Views/{1}/OtherPath/{0}.master", 
       "~/Views/{1}/{0}.master", 
       "~/Views/Shared/OtherPath/{0}.master", 
       "~/Views/Shared/{0}.master" 
      }; 
    } 



     protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath) 
     { 
      return new LocalizationWebFormView(viewPath, masterPath); 
     } 

     protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath) 
     { 
      return new LocalizationWebFormView(partialPath, null); 
     } 
} 
+0

不重寫ViewEngine不可能使用?像<%Html.RenderPartial(「〜/ Views/User/Users.ascx」,Model); %>,但會引發錯誤。 – Prasad 2010-02-04 17:38:03

+0

我不這麼認爲。在錯誤中您會看到搜索到的路徑。爲了它的樂趣,你可以嘗試「../../User/Users.ascx」,但那真是凌亂。重寫視圖引擎是你應該害怕的。 – 2010-02-04 18:42:07

相關問題