Error when including ViewPage on razor page

I get the following error when wrapping my model with ViewPage

Property ‘ViewData’ is of type ‘Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary1[[Alert.Web.ServiceModel.Types.Entity, Alert.Web.ServiceModel, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]', but this method requires a value of type 'Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary1[[System.Object, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]’. Parameter name: viewContext

Here is the code in question:

using Alert.Web.ServiceModel
@inherits ViewPage<Alert.Web.ServiceModel.Types.Entity>


     @{
         ViewBag.Title = "Web app doing buildy stuff";
     }
     <ul>
         <li><a href="/create/entity">Create Entity</a></li>
         <li><a href="/create/event">Create Event</a></li>
         <li><a href="/create/action">Create Action</a></li>
    
    </ul>
     @Html.Partial("AutoGrid", Db.Select<Alert.Web.ServiceModel.Types.Entity>())

Any ideas would be great.

If this is in .NET Core it sounds like you’re running into the issue where Razor Pages ASP.NET Core doesn’t properly support traditional inheritance. https://github.com/aspnet/Mvc/issues/5397

See this workaround: https://github.com/aspnet/Mvc/issues/5397#issuecomment-261124519

That is exactly my issue and should have paid better attention to the RazorRockstars example. Your work around resolved my issue. Thanks.

This issue resolved for my main for the page in question but I have other views in the Views folder that now display this same error. I have another copy of _viewImports.cshtml in the directory but that hasn’t resolved the problem.

Here is my viewimports:

@inherits ViewPage<TModel>

@using ServiceStack
@using ServiceStack.Mvc
@using ServiceStack.Text
@using Alert.Web
@using Alert.Web.ServiceModel
@using Alert.Web.ServiceModel.Types
@using System.Linq
@using ServiceStack.OrmLite
@using Alert.Web.ServiceInterface

@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

There is the top of the file in question. Please note all the files in this directory have the same issue:

@model Event

<div class="modal fade" id="modalDelete">
    <div class="modal-dialog modal-sm">
        <form id="formDelete" action="@(new DeleteEventRequest() {Id = Model.Id}.ToPostUrl())">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    <h4 class="modal-title">Confirm Delete</h4>
                </div>
                <div class="modal-body">
                    Did you really want to do delete this @(Model.GetType().Name)?
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                    <button type="submit" class="btn btn-danger">Delete @(Model.GetType().Name)</button>
                </div>
            </div>
        </form>
    </div>
</div>

Note we don’t have control over ASP.NET Core MVC’s Razor implementation which is used to render Razor views in .NET Core, if you’re running into another limitation of .NET Core’s implementation.

But if you can put together a stand-alone repro I can run locally to reproduce the issue, I can spend some time looking into it to see if I can find another workaround.

I can share a link to the private repo as it would be too hard to reproduce. As long as you promise not to post the code to Github :wink:

I’d prefer not to have any access to any confidential source code. Can’t you isolate the issue into a small stand-alone repro that doesn’t contain any sensitive info? We’re not going to be able to change the behavior as all compilation inc _ViewImports.cshtml and _ViewStart.cshtml is all handled internally by MVC, but if I had a small stand-alone repro I can at least spent some time trying to debug and try some trial/error to see if there’s any potential workarounds, but this would be much harder if this was part of a bigger project, instead of the Minimal, Complete, and Verifiable example needed.

Understood and very reasonable. I will see what I can do.

I have narrowed the issue to trying to include the AutoGrid partial onto my page. Removing it renders the page properly. I couldn’t find any examples of NetCoreApps using the AutoGrid so maybe it behaves differently with netCore.

It’s just a simple self-encapsulated partial, that’s all HTML except for the inherits declaration and the @Model.AsRawJson() extension method.

Have you tried changing the model declaration to @model System.Collections.IEnumerable ?

Not sure why I did not try that but adding the model declaration got it to finally worked.

1 Like