ViewModel MVC 5 2个模型,数据库,控制器视图关系的理解 [英] ViewModel MVC 5 2 models, db, controller view relationship understanding

查看:55
本文介绍了ViewModel MVC 5 2个模型,数据库,控制器视图关系的理解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力探索整个概念,我真的可以使用您的帮助人员.我正在使用代码优先方法.我昨天在此处发表了一篇文章但是基于建议的进一步工作对我没有用.因此,为了使问题更清楚,我再次进行了检查.

I keep trying to get my head around this whole concept and i could really use your help guys. I'm working from code first approach. I made a post yesterday here but further work based on advises didn't work for me. So to make the problem clear i go over it again.

我有2个简单的课程.

 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; }
}

对于每个人,我都有自己的控制器和EF生成的CRUD视图集.因此,我可以在数据库中创建和存储这两个对象,为此我有一个上下文类:

For each of them i have its own controller and set of CRUD views genereated by EF. So i can create and store this two objects in db, i have a context class for that:

public class EventsContext : DbContext
    {
        public DbSet<Media> Medias { get; set; }
        public DbSet<Video> Videos { get; set; }
        }   
    }

我在MediasController中有此方法

I have this method im MediasController

public ActionResult MediaMain()
{
    IEnumerable<Media> medias = db.Medias;
    ViewBag.Medias = medias;
    return View();
}

它从db访问存储的对象,并允许处理视图中的每个字段.这样.

it accesses stored object from db and allows to work with every field in view. this way.

@foreach (var b in ViewBag.Model)
{ 
   @Html.Raw(Model.ImagePath)
}

很好,我知道.但.我仍然有两种不同的观点,而我只需要一种.因此,我创建了一个MediaViewModel类(感谢帮助),如下所示:

Good and sound, i understand that. BUT. i still have two different views, while i need only one. So i created a MediaViewModel class (thanks for help with that) looking like this:

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

如果我在MediasController中创建一个方法,则如下所示:

If i create a method in MediasController looks like this:

public ActionResult MediaMain () {
    var model = new MediaViewModel();
    return View(model);
}

我无法从对象访问数据,因为它给了我nullexception,好像我没有通过对象发送.我认为我需要类似 IEnumerable< MediaViewModel>之类的东西.medias = db.MediaViewModel; 但我的数据库上下文中没有这个,我需要创建一个吗?如果是这样,那将是两个类的对象的集合?听起来不对.我需要查看Controller中到底发生了什么,并能够访问我需要的所有数据.请帮助.

i cant access data from objects, as it gives me nullexception, looks like i'm not sending over an object. I think i need to have something like IEnumerable<MediaViewModel> medias = db.MediaViewModel; but i don't have this in my db context, do i need to create one? If so, it would be a collection of object of two classes? That's does not sound right. I need to see what exactly happening in Controller and in view to be able to access all data i need. Pls help.

推荐答案

您的模型

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

您的控制器方法:

public ActionResult MediaMain () {
    var model = new MediaViewModel();
    model.media = db.Medias.ToList();
    model.video = db.Videos.Tolist();
    return View(model);
}

希望有帮助.

这篇关于ViewModel MVC 5 2个模型,数据库,控制器视图关系的理解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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