1

I have the following 3 files (note: only relevant data is displayed):

~/Controllers/HomeController.cs

namespace MyApplication.Controllers {
    public class HomeController : Controller {
        public ActionResult Index() {
            return View();
        }
    }
}

~/Views/Home/Index.cshtml

@Html.Partial("~/Areas/AccountManager/Views/Account/_RegisterPopup.cshtml")

~/Areas/MyArea/Views/Account/_RegisterPopup.cshtml

@Html.Partial("_PasswordInfo", null, new ViewDataDictionary())

~/Areas/MyArea/Views/Shared/_PasswordInfo.cshtml

@using MyApplication.Areas.MyArea
<h2>PasswordInfo</h2>

When I'm hovering my mouse above the Html.Partial call in the _RegisterPopup file, it shows (view) ~/Areas/MyArea/Views/Shared/_PasswordInfo.cshtml and it should be able to find this view.

When I run the application and navigate to the Home/Index page (which will call the HomeController Index-method) it will fail to find the _PasswordInfo partial and throw an exception. The exception shows its only searching ~/Views/Home/ and ~/Views/Shared/. Where it should have searched in ~/Areas/MyArea/Views/.

I think the problem here is that since the HomeController in the main application (rather than a controller in the area) is executing the rendering here, it will only search in the Views in the main application instead of its areas. What would be the best way to solve this issue?

I'd like to keep inexplicitly reference the _PasswordInfo partial, so I won't have to worry about the references when I change its path. But referencing the partial by providing the fullpath @Html.Partial("~/Areas/MyArea/Views/Shared/_PasswordInfo.cshtml", null, new ViewDataDictionary()) rather than only its name @Html.Partial("_PasswordInfo", null, new ViewDataDictionary()) prevents the exception being throwed, as expected ofcourse.

Would it still be best to use this approach? Would it be better to expand the view engine to always search areas? Or is there some kind of way to make the ViewEngine understand it should search in Area views in this situation?

Kevin
  • 2,760
  • 3
  • 15
  • 30
  • 1
    I think your question is similar to this. This will help [https://stackoverflow.com/questions/4390045/asp-net-mvc-render-a-partial-view-from-an-area](https://stackoverflow.com/questions/4390045/asp-net-mvc-render-a-partial-view-from-an-area) – Jaydeep Karena May 24 '18 at 11:24

0 Answers0