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

  • SEARCH
  • Home
  • 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 495963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:38:28+00:00 2026-05-13T05:38:28+00:00

I am working on an ASP.Net MVC website and I am stuck on a

  • 0

I am working on an ASP.Net MVC website and I am stuck on a getting my C# code using generics to play nice with my views.

I have a Controller that passes a Model to a View (so far so good). The Model is of type IDescribedEnumerable<T> with some constraints on T, among those is the constraint that T inherits from an interface (IDescribedModel).

I can easily write a View that accepts an IDescribedEnumerable<Country> and that will work as long as T is in fact the type Country.

However, I’d also like to write a default view that accepts an IDescribedEnumerable of <<whatever>> and that will render it. This should be entirely possible. I don’t always need to know the specific type of the Model. Often just knowing that it’s an IDescribedModel is enough.

As long as I stay in C# there is no problem. When I don’t care about the specific type I just declare methods and objects as accepting a <T>. When I do care I declare them as accepting Country.

But:
(1) If I want to render a View I have to pick an type. I can’t just say Inherits="System.Web.Mvc.ViewUserControl<IDescribedEnumerable<T>>" I have to specify an existing type between the <>. (even if I were to inherit from ViewUserControl I’d have to cast it to an IDescribedEnumerable<<something>>.
(2) Ideally I’d say that Model is IDescribedEnumerable<IDescribedModel> in the default View and IDescribedEnumerable<Country> in the specific implementation. However, then my Controller needs to know whether he’s going to render to the default View or the specific view. It is not possible to simply cast an object that is IDescribedEnumerable<Country> to IDescribedEnumerable<IDescribedModel>. (IIRC it is possible in C# 4, but I’m using 3.5)

So what should I do? All options I can think of are very sub-optimal (I’m not looking forward to removing the generics and just casting objects around, nor to copy pasting the default view 65 times and keeping the copies synchoronized, nor going reflection gallore and creating an object based on a known Type object)

  • 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-13T05:38:28+00:00Added an answer on May 13, 2026 at 5:38 am

    while awaiting some C# genius to come along with the answer to all my problems I have implemented the trick that IEnumerable also uses:

    I added a method public IDescribedEnumerable<IDescribedModel> AsIDescribedModels() to the IDescribedEnumerable interface and created a new class GenericDescribedEnumerable<T> : IDescribedEnumerable<IDescribedModel>. In my DescribedEnumerable<T> class I create a GenericDescribedEnumerable and return that. In the GenericDescribedEnumerable I return this

    in full code:

    public interface IDescribedModel<T> : IDescribedModel{
        T original {
            get;
        }
    }
    
    public interface IDescribedEnumerable {
        IDescribedEnumerable<IViewModel> AsIViewModels();
    }
    
    public interface IDescribedEnumerable<T> : IDescribedEnumerable
        where T : IDescribedModel{
        IEnumerable<T> GetViewModels();
    }
    
    public class DescribedEnumerable<T> : IDescribedEnumerable<IDescribedModel<T>>{
    
        public DescribedEnumerable(IEnumerable<T> enumerable) {}
    
        public IDescribedEnumerable<IViewModel> AsIViewModels() {
            return new GenericDescribedEnumerable<T>(/*allProperties*/);
        }
    
        public IEnumerable<T> GetViewModels() {
            foreach ( T obj in _enumerable ) {
                var vm = new DescribedModel<T>( obj);
                yield return vm;
            }
        }
    }
    
    public class GenericDescribedEnumerable<T> : IDescribedEnumerable<IViewModel>{
        //pass in the constructor everything you need, or create in the 
                //constructor of DescribedEnumerable<T>
        public GenericDescribedEnumerable(/*allProperties*/) {
        }
    
        public IEnumerable<IViewModel> GetViewModels() {
            foreach ( T obj in _enumerable ) {
                var vm = new PlatoViewModel<T>( obj );
                yield return vm;
            }
        }
    
        public IDescribedEnumerable<IViewModel> AsIViewModels() {
            return this;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working in ASP.NET MVC 1.0 using MvcContrib FluentHtml version 1.0. The following code
I'm working on a new blog website created using ASP.NET MVC. I'm wondering what
I have a ASP.net MVC2 website that was working great when using Visual Studio
I'm using ASP.NET MVC 3 code-first and I have added validation data annotations to
I am working on a website in asp.net mvc. I have a route routes.MapRoute(
I am working on a website in ASP .NET MVC 3 using Razor and
I am working on a ASP.NET MVC website using Nhibernate as my ORM. The
i've started working with ASP.NET MVC and using Entity Framework, concretely with SQLite db,
So I am working with ASP.Net MVC + jQuery autocomplete and I have a
background:Me and my coworkers are working on asp.net mvc project ... we have a

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.