Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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??

like image 991
kingrichard2005 Avatar asked Dec 07 '22 04:12

kingrichard2005


2 Answers

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

like image 199
David Morton Avatar answered Dec 28 '22 18:12

David Morton


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>"
like image 45
William Edmondson Avatar answered Dec 28 '22 19:12

William Edmondson