Multiple inheritance of ASP.Net MVC in a view

I am trying to figure out if it is possible to have multiple inheritance in a view in ASP.Net MVC. Now I am trying to print a list of records from two different tables from my model in one 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 course in the table as follows:

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

I am not sure how this can be done, any help or suggestions are appreciated.

UPDATE:

Thanks to everyone 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 opinion, now I 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 they are listed in my view, because I only inherit directly from CoursePrefix. I'm not sure what I am missing. Do I need to have a constructor for a composite object? Do I need inheritance and implementation instructions for CoursePrefix and ICourse, respectively, if I already supposedly disclose the properties of each of the shell-list classes?

+4
source share
5 answers

Create a ViewModel class that has properties that display both datasets. Bind to it.

+8
source

A model can contain only one object. If you have several objects that you need for your view, you will need to create a compound object.

It can be as simple as exposing several properties that correspond to the types of objects 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>" 
+4
source

This is not inheritance, these are generics, very different.

No, this is not possible, you need to combine them into a wrapper class containing two links, or just add a link to CoursePrefix in the Course class, it would seem reasonable, but I base this on a very very limited understanding of your model!

+2
source

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

+1
source
  • If both are derived from a generic type, you can have a look of this type. Then you may need to check the type in several places and display the type properties defined after casting, while displaying all the usual properties in the normal mode. The downside is that the presentation may be considered too smart in this case.
  • If these are really different types, you should create two different views, which seems to be a more object oriented way.
  • You can create a composite view of these two types. I would be against this, since one of them can always be zero.

The real question is why are you trying to print two different tables in one view. If both of these tables are always filled, go to it and create a composite DTO, otherwise they look like separate views / one kind of common base.

0
source

Source: https://habr.com/ru/post/1302793/


All Articles