Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6019327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:24:21+00:00 2026-05-23T03:24:21+00:00

I have an abstract class foo that contains some properties and methods. I have

  • 0

I have an abstract class foo that contains some properties and methods. I have also have a number of classes that implement foo such as barfoo and foobar.

To safe rewriting the same code over multiple different views I’ve created a strongly typed partial view

@model List<foo>
@foreach (var x in Model) {
<div> x.SomeProperty </div>
}

Sadly, when I attempt to call this view with

@{ Html.RenderPartial("PartialView", List<Foobar>); }

I get the following error

The model item passed into the dictionary is of type System.Collections.Generic.List1[Namespace.Models.Foobar]', but this dictionary requires a model item of type 'System.Collections.Generic.List1[Namespace.Models.Foo]’.

Is what I’m trying to do possible at all or will I just have to duplicate code?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T03:24:22+00:00Added an answer on May 23, 2026 at 3:24 am

    Trying exactly what you’re trying isn’t possible. If all you’re doing is enumerating over the collection (rather than modifying it), you could change to the following and things should be ok:

    @model IEnumerable<foo>
    @foreach (var x in Model) {
        <div> x.SomeProperty </div>
    }
    

    The problem is fairly easy to understand if you really think about it.

    Your Model is a List<foo>. That means that it should be entirely possible to say Model.Add(new Barfoo());.

    You’re trying to pass a List<Foobar> as the Model. Obviously, Model.Add(new Barfoo()); would fail in that case because it’s not possible.

    Using a covariant interface like IEnumerable allows you to do exactly what you need.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an abstract class, Foo, that has a non-abstract method called Bar. I
We have an abstract class that is the base for a number of different
I have a class which is not thread safe: class Foo { /* Abstract
In my domain model I have an abstract class CommunicationChannelSpecification, which has child classes
If I have an abstract class in java named Foo and it has an
First let's establish this. I have public abstract class Foo { public static void
I have an abstract generic class public abstract class Foo<TType> with an abstract method
I want to have an abstract class with 3 classes extending it which i
I have an abstract Class Monitor.java which is subclassed by a Class EmailMonitor.java .
I have an abstract class and I would like to use it quickly by

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.