Home > Enterprise >  Passing concrete type to view with interface as model
Passing concrete type to view with interface as model

Time:10-01

In my .NET 4.6.1 MVC project I have a partial view that has an Interface called IPaginatedList as the model.

_Pager.cshtml

@model Shared.Abstract.IPaginatedList

In my main view I am calling the _Pager partial view while passing in an object called Medals which is of type PaginatedList

Index.cshtml

@Html.Partial("_AjaxPager", Model.Medals)

Medal Object

public PaginatedList<Medal> Medals { get; set; }

The PaginatedList class is setup as follows

public class PaginatedList<T> : List<T>, IPaginatedList
{
}

You can see that it inherits from IPaginatedList and a generic List<T>.

I'm transfering my code to .NET Core 3.1 and I am getting an error when using this exact same code.

The model item passed into the ViewDataDictionary is of type 'PaginatedList`1[MedalViewModel]', but this ViewDataDictionary instance requires a model item of type 'Shared.Abstract.IPaginatedList'.

What do I have to do to resolve this issue?

CodePudding user response:

It should work correctly cause PaginatedList<T> is a IPaginatedList. Check that you don't have duplicate IPaginatedList interfaces and if you do - either delete extra ones or make sure that correct namespace is used in both places (model and view).

  • Related