kingrichard2005
kingrichard2005

Reputation: 7269

ASP.Net MVC multiple inheritance in a View

I'm trying to figure out if its possible to have multiple inheritance in a view in ASP.Net MVC. Right now I'm trying to print out a list of entries from two different tables from my model in a single View. I have the following line at the top of my view:

Inherits="System.Web.Mvc.ViewPage<List<GEApproval.Models.CoursePrefix>>"

But I also want to include the table Course as follows:

Inherits="System.Web.Mvc.ViewPage<List<GEApproval.Models.Course>>"

I'm not sure how this can be accomplished, any help or suggestions are appreciated.

UPDATE:

Thank you all for your help, I went ahead and created a composite class as follows:

    namespace GEApproval.Models
{
    public class Listings: GEApproval.Models.CoursePrefix, GEApproval.Models.ICourse
    {
        public List<CoursePrefix> CoursePrefixObjList { get; set; }
        public List<Course> CourseObjList { get; set; }
        private GEApprovalDataModel _db;

        //Constructor
        public Listings()
        {
            _db = new GEApprovalDataModel();
        }

        //Generate a list of all courses associated with the prefix and place in ViewData model
        public void listCourses(ViewDataDictionary viewData, int prefixID)
        {
            var test = _db.CoursePrefix.Include("Course").First(cp => cp.id == 1);
            //Show total courses for this prefix
            viewData.Model = test.Course.ToList();
            viewData["prefix"] = test.Prefix;
            viewData["courseCount"] = test.Course.Count;
            int courseCount = test.Course.Count();//Test
        }

    }
}

And in my view, I now have the following line:

Inherits="System.Web.Mvc.ViewPage<List<GEApproval.Models.Listings>>"

I'm still a little confused because I still cannot access the properties of the Course object when listing them in my view, because I'm only inheriting directly from CoursePrefix. I'm not sure what I'm missing. Do I need to have a constructor for the composite object? Do I need the inherit and implementation statements for CoursePrefix and ICourse respectively if I'm already, supposedly, exposing the properties of each within the Listings wrapper class??

Upvotes: 4

Views: 9918

Answers (5)

jeroenh
jeroenh

Reputation: 26782

There is no such thing as multiple inheritance in .Net. As the other answers have mentioned, use a composite ViewModel object for this situation (this is generally considered a much better design choice, even in languages that support it).

Upvotes: 1

William Edmondson
William Edmondson

Reputation: 3637

You model can only contain one object. If you have multiple objects you need for your view you will have to create a composite object.

This can be as simple as exposing multiple properties that match the object types needed in the view.

public class ModelObj
{
   public List<CoursePrefix> CoursePrefixObjList {get; set;}
   public List<Course> CourseObjList {get; set;}
}

Then just use your ModelObj in the view

Inherits="System.Web.Mvc.ViewPage<ModelObj>"

Upvotes: 4

Yuriy Faktorovich
Yuriy Faktorovich

Reputation: 68667

  • If both of them are derived from a common type you could have your view be of that type. Then you may need to check the type in a few places and output the type specific properties after casting, while outputting all the common properties normally. The down side is that the View may be considered too smart in this case.
  • If these are really separate types, you should create two different Views, seems the more object oriented way to go.
  • You could create a composite View of these two types. I'd be against it, as one of them might always be null.

The real question is why you're trying to print out two different tables in the same View. If both of these tables are always filled then go for it and create the composite DTO, otherwise these seem like separate Views / one View of a common base.

Upvotes: 0

Paul Creasey
Paul Creasey

Reputation: 28824

This is not inheritance, it's generics, very differant.

No it isn't possible, you need to combine them into a wrapper class containing two references, or simply adding a reference to CoursePrefix within the Course class would seem reasonable, but i base that on a very very limited understanding of your model!

Upvotes: 2

David Morton
David Morton

Reputation: 16505

Create a ViewModel class that has properties that expose both sets of data. Bind to that instead.

Upvotes: 8

Related Questions