Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PartialView must derive from WebViewPage error

I am getting "must derive from WebViewPage" error when calling PartialView inside other folder instead Views folder.

Error

System.InvalidOperationException: The view at '~/Modules/HtmlContent/_HtmlContent.cshtml' must derive from WebViewPage, or WebViewPage<TModel>.
PartialView adding

Page.cshtml

<div class="side">
    @Html.Action("Side")
</div>

HomeController.cs

public ActionResult Side()
{
    return PartialView("~/Modules/HtmlContent/_HtmlContent.cshtml");
}

File Hierarchy

Modules
|
+-- HtmlContent
|             |
|             +-- _HtmlContent.cshtml
|
Views
    |
    +-- Home
    |      |
    |      +-- Index.cshtml
    |      |
    |      +-- Page.cshtml
    |
    +-- Shared
             |
             +-- Layout.cshtml
             |
             +-- _Partial.cshtml
like image 800
user2282567 Avatar asked Jun 10 '13 06:06

user2282567


1 Answers

You need to put a web.config in the root of this custom folder. The same as ~/Views/web.config. Inside this web.config you will find a section which indicates the type of the Razor pages:

<system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
      <namespaces>
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Optimization"/>
        <add namespace="System.Web.Routing" />
      </namespaces>
    </pages>
</system.web.webPages.razor>

Notice how the pageBaseType is set to System.Web.Mvc.WebViewPage.

Alternatively you could add the following to the top of your Razor views:

@inherits System.Web.Mvc.WebViewPage
like image 118
Darin Dimitrov Avatar answered Sep 22 '22 06:09

Darin Dimitrov