2010-02-04 57 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

相关问题