使用Html.RenderPartial获取其他视图/文件夹中的文件

时间:2010-02-04 06:40:58

标签: asp.net-mvc renderpartial

如何在其他文件夹中使用Html.RenderPartial作为PartialView?

我试过:

<% 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?

1 个答案:

答案 0 :(得分:0)

如果要更改查找partials,vieww或母版页的规则,则必须更改View Engine。

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);
      }
}