使用2个类创建ViewModel MVC 5,使用它的控制器 [英] ViewModel MVC 5 created using 2 classes, controller which using it

查看:56
本文介绍了使用2个类创建ViewModel MVC 5,使用它的控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这不是一个新话题,但是即使通读了其中许多内容,我仍然不完全了解如何在我的情况下实现它们.寻找您的建议.

It is not a new topic, however even after reading through many of them, i'm still not fully understand how i can implement them in my case. looking for your advise.

我有两节课.

public class Media
    {
        public int Id { get; set; }
        public string title { get; set; }
        public string description { get; set; }
        public string body { get; set; }
        public string ImagePath { get; set; }
    }

public class Video
    {
        public int ID { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        public string VideoLink { get; set; }
        public string tags { get; set; }
    }

目前我有两个控制器,分别用于MediasControllerVideosController,它们每个都可以在MediaMain视图和VideoMain视图中工作.结果,我可以在两个单独的视图上显示两个对象.我们正在谈论的是一个媒体博客网站,新闻可能有所不同,它们可能只是文本,文本+图片或文本+视频.每个新的媒体"应填充在一个视图上.

At the moment i have two controllers for each of them MediasController and VideosController, each of them works with MediaMain view and VideoMain view. In result i can show two objects on two separate views. We are talking about sort of a media blog website, were news may differ, they may be text only,text + image or text + video. Every new "media" should populate on One view.

如果我们在高层次上进行讨论,我认为我应该创建一个新的viewModel类,以将需要在viewmodel页面上显示的那些字段收集到其中,对吗?这是我所拥有的:

If we are talking on a high level, i think i should create a new viewModel class to gather into it those fields which i need to show on viewmodel page, right? here is what i have:

public class MediaViewModel
    {
        public class Media 
        { 
                public string title { get; set; }
                public string description { get; set; }
                public string body { get; set; }
                public string ImagePath { get; set; }
        }
        public class Video 
        {
            public string Title { get; set; }
            public string Description { get; set; }
            public string VideoLink { get; set; }
        }
}

现在,如果正确,我应该有一个由此类制成的ViewModelController,对吗?还是我需要两个课程的集合? 我的意思是这样的:

Now, if that correct i should have a ViewModelController made of this class, right? Or maybe i need a collection of both classes? What i mean is something like this:

public class MediaViewModel
        {
            public class Media 
            { 
                    public string title { get; set; }
                    public string description { get; set; }
                    public string body { get; set; }
                    public string ImagePath { get; set; }

                    **public Video Video{ get; set; }**
            }
    }

请帮助.

推荐答案

您处于正确的轨道,除了您的类定义应如下所示:

You are on the right track, except your class definition should look like this:

public class MediaViewModel
{
   public Media media { get; set; }
   public Video video { get; set; }
}

这篇关于使用2个类创建ViewModel MVC 5,使用它的控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆