简体   繁体   English

ASP.Net MVC在View中的多重继承

[英]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. 我试图弄清楚它是否可能在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. 我仍然有点困惑,因为在我的视图中列出它们时仍然无法访问Course对象的属性,因为我只是直接从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?? 如果我已经(据说是)公开了每个列表包装类中的每个属性,我是否需要分别为CoursePrefix和ICourse的继承和实现语句?

Create a ViewModel class that has properties that expose both sets of data. 创建一个ViewModel类,该类具有公开这两组数据的属性。 Bind to that instead. 绑定到那个。

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 然后在视图中使用ModelObj

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

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! 不,这是不可能的,你需要将它们组合成一个包含两个引用的包装类,或者只是在Course类中添加一个对CoursePrefix的引用似乎是合理的,但我基于对你的模型非常有限的理解!

There is no such thing as multiple inheritance in .Net. 在.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 ). 正如其他答案所提到的,在这种情况下使用复合ViewModel对象(这通常被认为是一种更好的设计选择, 即使在支持它的语言中也是如此 )。

  • 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. 不利的一面是,在这种情况下,View可能被认为太聪明了。
  • 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. 如果这两个表总是被填充,那么去它并创建复合DTO,否则这些看起来像是单独的视图/一个共同基础的视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM