开发者

using Html.RenderPartial for the files in other Views/folders

How to use Html.RenderPartial for the PartialViews in other Folders?

I have tried as:

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

It thrown an error:

System.InvalidOperationException: The partial view '~/Views/User/Users.ascx' was not found. The f开发者_如何学运维ollowing 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

Is anything missing here or its not able to call partialview in other folders?


If you want to change the rule for finding partials, vieww or masterpages you have to change the 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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜