将不止一个模型返回到视图?

Returning more than just one model to the View?

我是 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 但我不知道如何将我的其他查询获取到视图以便我也可以从中获取数据.


更新

我正在为我的数据查询自定义 class,但我不知道如何设置 TechProfile

的 属性

现在我有

public IEnumerable<TechProfile> ProjectTechProfile { get; set; }

在我的习惯中 class,但是它不起作用,所以我想我必须指定 TechIdNameElements?
但是怎么办?

ViewModel 环绕着您通过 2 个查询获得的 2 个模型,因此您可以 return 将其作为视图的单个对象。对于您的情况,我们需要先解决另一个问题。您在数据查询中 returning 了一个匿名对象。

这意味着,您的数据查询需要return强类型对象而不是匿名对象。

为您的数据查询创建 class:

public class MyCustomDataObject
{
    public int ProjectId { get; set; }
    //... map all properties as needed
}

然后将您的数据查询编辑为 return 这个对象:

    var Data = from a in db.Member
               where a.Person.PersonId.Equals(Id)
               select new MyCustomDataObject
               {
                   ProjectId =  a.Project.ProjectId,
                   //assign all properties
               };

现在您需要创建实际的 ViewModel class:

public class MyViewModel
{
    public IEnumerable<Person> Persons { get; set; }
    public IEnumerable<MyCustomDataObject> Data { get; set; }
}

之后,您只需在 Actionmethod 中为其分配值即可:

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 MyCustomDataObject
               {
                   ProjectId = a.Project.ProjectId,
                   //...
               };

    //store data of both queries in your ViewModel class here:
    var vm = new MyCustomDataObject();
    vm.Persons = People;
    vm.Data = Data  
    //return ViewModel to View.
    return View(vm);
}

然后在你的视图中声明:@model Namespace.Subfolder.MyCustomDataObject

您可以在视图中使用@Html.Action("actionName","controllerName")方法。您可以将原始视图划分为多个局部视图,然后您可以使用 @Html.Action("actionName","controllerName") 方法使用动态模型绑定渲染该局部视图。

有关示例代码的更多详细信息http://devproconnections.com/development/how-use-aspnet-mvc-render-action-helpers

您可以使用如下方法在单个视图中获取多个模型

private IList<People> GetPeople()
{
    return from a in db.Person
                 select a;
}

private IList<Data> GetData()
{
    return 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
               };

}

public ActionResult Index(int Id)
{
    var MultipleModel = new Tuple<IList<People>,IList<Data>>(GetPeople(),GetData()) { };
    return View(MultipleModel);
}

这里是a codeproject tutorial on the subject