MVC - 视图中的多个模型 [英] MVC - Multiple models in a view

查看:102
本文介绍了MVC - 视图中的多个模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MVC(第一次)与实体框架,数据库首先



我想做的是在单个视图中显示数据库中的数据。我首先创建了数据库,然后从包含所有表的数据库中创建了一个ADO.NET实体数据模型。然后,我创建了一个以实体数据模型为模型的强力类型的索引视图。



在我的索引中,我的顶部

  @model IEnumerable< Forum6.Models.Forum> 

这允许我从数据库中的论坛表中获取行。如果我尝试添加一个额外的模型,我得到这个错误消息,当我运行:


 第1行:@model IEnumerable< Forum6.Models.Forum> 
第2行:@ model2 IEnumerable< Forum6.Models.Post>

解析器错误消息:文件中只允许一个模型语句。


在寻找答案后,我发现:(如果你不想去那个路线,就忽略自动机器部分)



您还需要在ViewModel中输入实际数据,因为

  ViewModel db = new ViewModel(); 

public ActionResult Index()
{
return View(db);
}

只会让您的视图成为一个空的ViewModel。
一种方式是。

  public ActionResult Index()
{
var model = new ViewModel
{
Forum = db.GetForum(),
Post = db.GetPost(),
主题=你得到想法
};

return View(model);
}

最后一件事情是,命名属性或变量一般应该使用复数动词当它包含一个列表。所以你的ViewModel将是。

  public class ViewModel 
{
public IEnumerable&Forum.Models.Forum> ;论坛{get;组; }
public IEnumerable< Forum6.Models.Post>帖子{get;组; }
public IEnumerable< Forum6.Models.Topics>主题{get;组; }
public IEnumerable< Forum6.Models.Users>用户{get;组; }
public IEnumerable< Forum6.Models.PrivMsg> PrivMsgs {get;组; }
public IEnumerable< Forum6.Models.Permission>权限{get;组; }
}


I'm using MVC (for the first time) with Entity framework, Database first

What I want to do is display data from a database in a single view. I created the database first, then I made a ADO.NET Entity Data Model based from the database that contains all the tables. I then created a Index view that was strongly typed with my Entity Data Model as model.

In my Index I have at the top

@model IEnumerable<Forum6.Models.Forum>

This allows me to get the rows from the table "Forum" from my database. If I try to add an extra model I get I get this error message when I run:

Line 1: @model IEnumerable<Forum6.Models.Forum>
Line 2: @model2 IEnumerable<Forum6.Models.Post>  

Parser Error Message: Only one 'model' statement is allowed in a file.

After searching for an answer I found this: Two models in one view in ASP MVC 3 The answer was to create a ViewModel (ParentModel) that contained all the Models (Tables). This is the ViewModel I created:

public class ViewModel
{
    public IEnumerable<Forum6.Models.Forum>         Forum       { get; set; }
    public IEnumerable<Forum6.Models.Post>          Post        { get; set; }
    public IEnumerable<Forum6.Models.Topics>        Topics      { get; set; }
    public IEnumerable<Forum6.Models.Users>         Users       { get; set; }
    public IEnumerable<Forum6.Models.PrivMsg>       PrivMsg     { get; set; }
    public IEnumerable<Forum6.Models.Permission>    Permission  { get; set; }
}

I edited my controller to look like this:

   public class HomeController : Controller
{
    // ForumDBEntities old_db = new ForumDBEntities();

    ViewModel db = new ViewModel();

    public ActionResult Index()
    {
        return View(db);
    }
}

Then replaced the old Index view with a new strongly typed view that used the ViewModel as model. Which contains:

@model IEnumerable<Forum6.Models.ViewModel>

Trying to run this gives me this error:

The model item passed into the dictionary is of type 'Forum6.Models.ViewModel', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[Forum6.Models.ViewModel]

How do I make the "ViewModel" enumarable? Or is my error elsewhere?

解决方案

You'll need to change @model IEnumerable<Forum6.Models.ViewModel> to @model Forum6.Models.ViewModel as you're wrapping your IEnumerables inside a single ViewModel.

A good rule of thumb is to have a 1:1 relationship between your ViewModel and View.

This might be a good read for you: http://lostechies.com/jimmybogard/2009/06/30/how-we-do-mvc-view-models/ (just ignore the automapper part if you don't want to go that route)

You'll also need to put in actual data in your ViewModel since

ViewModel db = new ViewModel();

public ActionResult Index()
{
    return View(db);
}

will just give your view an empty ViewModel. One way to do it would be.

public ActionResult Index()
{
    var model = new ViewModel
                    {
                        Forum = db.GetForum(),
                        Post = db.GetPost(),
                        Topic = you get the idea
                    };

    return View(model);
}

One last thing when naming properties or variables in general you should use the plural verb when it contains a list. So your ViewModel would be.

public class ViewModel
{
    public IEnumerable<Forum6.Models.Forum>         Forums      { get; set; }
    public IEnumerable<Forum6.Models.Post>          Posts       { get; set; }
    public IEnumerable<Forum6.Models.Topics>        Topics      { get; set; }
    public IEnumerable<Forum6.Models.Users>         Users       { get; set; }
    public IEnumerable<Forum6.Models.PrivMsg>       PrivMsgs    { get; set; }
    public IEnumerable<Forum6.Models.Permission>    Permissions { get; set; }
}

这篇关于MVC - 视图中的多个模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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