views:

231

answers:

1

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

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

A: 

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);
      }
}
Malcolm Frexner
Is it not possible of using without overriding ViewEngine? something like <% Html.RenderPartial("~/Views/User/Users.ascx", Model); %>, but it throws error.
Prasad
I dont thinks so. In the error you see the paths that are searched. For the fun of it you could try "../../User/Users.ascx" but thats realy messy. Overriding the viewengine is nothing you should be afraid of.
Malcolm Frexner