如何使用多种模型在ASP.NET MVC一种看法? [英] How to use multiple models in one view for ASP.NET MVC?
问题描述
我读过关于SO类似的问题,但似乎无法弄清楚这个问题,这是特定的DbContext对象(我认为)。下面是一些伪code来说明。
I've read similar questions on SO but can't seem to figure out this issue, which is specific to DBContext objects (I think). Here's some dummy code to illustrate.
我有以下的code在我的Index()动作:
I have the following code in my Index() action:
public ActionResult Index()
{
AnimalDBContext db = new AnimalDBContext();
return View(db.Dogs);
}
我有以下的code代表我的模型:
I have the following code for my models:
public class Dog
{
public int ID { get; set; }
public string name { get; set; }
public string breed { get; set; }
}
public class AnimalDBContext : DbContext
{
public DbSet<Dog> Dogs { get; set; }
}
在我看来,我有以下几点:
In my view I have the following:
@model IEnumerable<AnimalProject.Models.Dog>
@foreach (var d in Model)
{
<h3>@d.name</h3>
<h2>@d.breed</h2>
}
通过一切都在我的数据库每条狗的伟大工程,视图将循环。不过,我有,我想在同另外一个表中的另一组的DbContext数据。我希望能够在数据库中为该表枚举各项目为好。
Everything works great, the view will loop through every dog in my database. However, I have another set of DBContext data from another table that I want in the same view. I want to be able to enumerate each item in the database for that table as well.
这是我想要的,如果你明白我的意思:
This is what I want, if you catch my drift:
@model IEnumerable<AnimalProject.Models.Dog>
@model IEnumerable<AnimalProject.Models.Cat>
@foreach (var d in Dog)
{
<h3>@d.name</h3>
<h2>@d.breed</h2>
}
@foreach (var c in Cat)
{
<h3>@c.name</h3>
<h2>@c.breed</h2>
}
我已经试过类组合在一起,并使用部分观点,但显然你不能在一个局部视图不同的模式,因为我总是得到错误信息:
I have tried grouping the classes together and using a partial view, but apparently you can't have a different model in a partial view, because I always get the error message:
模型项目传入
字典是类型
System.Data.Entity.DbSet 1 [AnimalProject.Models.Dog]',
1 [AnimalProject.Models.Cat]'。
但本词典需要一个模型
类型项目
System.Collections.Generic.IEnumerable
"The model item passed into the dictionary is of type 'System.Data.Entity.DbSet
1[AnimalProject.Models.Dog]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable
1[AnimalProject.Models.Cat]'."
所以,我怎么可以使用多种模型在我看来,无论得到我从数据库中的单独的表所需的数据?
So, how can I use multiple models in my view that both get the data I want from separate tables in the database?
推荐答案
有关创建自定义视图模型类什么:
What about creating custom view model class:
public AnimalModel
{
public IEnumerable<Dog> Dogs { get; set; }
public IEnumerable<Cat> Cats { get; set; }
}
在指数
填写这一模式,并把它传递给将期望 AnimalModel
而不是可枚举的看法。
Fill this model in Index and pass it to the view which will expect AnimalModel
instead of enumerables.
编辑:
填充模型:
public ActionResult Index()
{
using (var db = new AnimalDBContext())
{
var model = new AnimalModel
{
Dogs = db.Dogs.ToList(),
Cats = db.Cats.ToList()
};
return View(model);
}
}
查看(我从来没有使用剃刀,所以我希望这是正确的):
View (I have never used Razor so I hope this is correct):
@model AnimalProject.Models.AnimalModel
@foreach (var d in Model.Dogs)
{
<h3>@d.name</h3>
<h2>@d.breed</h2>
}
@foreach (var c in Model.Cats)
{
<h3>@c.name</h3>
<h2>@c.breed</h2>
}
这篇关于如何使用多种模型在ASP.NET MVC一种看法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!