【发布时间】:2015-04-27 09:18:03
【问题描述】:
我对 MVC 还很陌生,我一直在阅读一些关于 ViewModels 的文章,但是我该如何将两个模型发送到我的 View,查询就像这样
public ActionResult Index(int Id)
{
var People = from a in db.Person
select a;
var Data = from a in db.Member
where a.Person.PersonId.Equals(Id)
select new
{
a.Project.ProjectId,
a.Project.Name,
a.Project.Customer,
a.Project.TechProfile.Select(x => new
{
x.TechId,
x.Name,
x.Elements
}),
a.MemberId,
a.Role,
a.Start,
a.End
};
return View(People);
}
我之前使用的是@model IQueryable<GeoCV.Models.Person>,所以我可以在我的视图中使用@foreach,但我不知道如何将我的其他查询获取到视图,以便我也可以从中获取数据。
更新
我正在为我的数据查询创建一个自定义类,但我不知道如何设置TechProfile 的属性
我现在有
public IEnumerable<TechProfile> ProjectTechProfile { get; set; }
在我的自定义类中,但它不起作用,所以我想我必须指定TechId、Name 和Elements?
但是怎么做呢?
【问题讨论】:
-
为它创建一个 ViewModel
-
您的视图模型需要包含属性
IEnumerable<Person>并且类型的另一个属性返回了您的第二个查询(为其创建单独的视图模型) -
如何为它创建一个 ViewModel?
标签: asp.net-mvc asp.net-mvc-3 model-view-controller