如何手动填充视图模型(不使用AutoMapper!) [英] How do I manually populate ViewModel (Not using AutoMapper!)
问题描述
我知道有很多关于这个问题的帖子,但我无法找到一个可以帮助我做我想做的。我知道我最终会使用Automapper但在我开始使用它,我想学习如何手工做的事情。我想创建一个视图模型,通过一个存储库的方式,从我的实体值填充它,然后将其发送给我的看法。就这么简单,这听起来,我有点吃力完成它。我使用MVC 3,EF 4.3,数据库优先。我已经自动生成我的班。我张贴有关单位(简称/改名为这个职位)和类,这里是我迄今为止:
I know there are a lot of posts on the subject but I cannot find one that helps me do what I want. I know that I will eventually be using Automapper but before I start playing with it, I want to learn how to do things manually. I want to create a ViewModel, populate it with values from my entities by way of a repository and send it to my View. As simple as this sounds, I am stuggling to get it done. I'm using MVC 3, EF 4.3, Database First. I have auto-generated my classes. I'm posting the relevant entities (abbreviated/renamed for this post) and classes, here is what I have so far:
总实体:航运标题
using System;
using System.Collections.Generic;
namespace My.Models
{
public partial class ShippingHdr
{
public ShippingHdr()
{
this.ShippingLI = new HashSet<ShippingLI>();
}
public int ID { get; set; }
public int ShipToSiteID { get; set; }
public Nullable<System.DateTime> DateShipped { get; set; }
public Nullable<System.DateTime> EstDeliveryDate { get; set; }
public string FromSitePOC { get; set; }
public Nullable<int> ShipperID { get; set; }
public string TrackingNo { get; set; }
public string Comments { get; set;}
public virtual Shippers Shippers { get; set; }
public virtual ICollection<ShippingLI> ShippingLI { get; set; }
}
}
下面是我的视图模型
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace My.Models.ViewModels
{
public class ShippingHeaderSummaryVM
{
public int ID { get; set; }
public string Site { get; set; }
public Nullable<System.DateTime> DateShipped { get; set; }
public Nullable<System.DateTime> EstDeliveryDate { get; set; }
public string TrackingNo { get; set; }
public string HeaderComments { get; set; }
public string Shipper { get; set; }
public int NumOrders { get; set; }
public string Site { get; set; }
}
}
下面是一个查询我回到我想用填充我的视图模型的项目。我相信这样做的最好的地方是在资源库中。我验证它返回我想用LinqPad(因此缺少参照我dbContxt)的数据。我只是不知道如何从查询中获取值的视图模型:
Here is a query I got to return the items I want to use to populate my Viewmodel with. I believe the best place for this is in a Repository. I verified it returns the data I want using LinqPad (hence the missing reference to my dbContxt). I just don't know how to get the values from the query to the ViewModel:
var shipments = from h in c.ShippingHdrs
where (h.ShippingLI.Count > 1)
join
e in c.vHr_Employees on h.CreatedBy equals e.ID
join
s in c.Shippers on h.ShipperID equals s.ShipperID
join
r in vAaiomsSites on h.ShipToSiteID equals r.SiteID
select new
{
h.ID,
r.Site,
h.EstDeliveryDate,
h.DateShipped,
h.TrackingNumber,
h.HeaderComments,
e.LastName,
h.ShippingLI.Count,
s.Shipper
};
所以,我不想使用Automapper做的,又是填充视图模型全部来自ShippingHdr实体的行并将其传递给我的看法。
So what I want to do, again without using Automapper, is to populate the ViewModel with all of the rows from the ShippingHdr entity and pass it to my view.
下面是需要被映射的filelds:
Here are the filelds that need to be mapped:
ShippingHeaderSummaryVM从出货量映射
ID = h.ID
Site = r.Site
DateShipped = h.DateShipped
EstDeliveryDate = h.EstDeliveryDate
TrackingNo = h.TrackingNumber
FromSitePOC = e.LastName
NumOrders = h.ShippingLI.Count
Shipper = s.Shipper
HeaderComments = h.HeaderComments
我在这里卡住了。
我如何填充从查询视图模型?
那么如何做我再从我的控制器调用的动作?
I am stuck here. How do I populate the ViewModel from the query? How then do I then call that action from my controller?
我希望我已经给了足够的信息,任何帮助将是AP preciated。
I hope I have given enough information, any help would be appreciated.
推荐答案
为了填充根据您的视图模型对象的出货量的列表,你需要创建一个映射方法从你的出货量收集从数据库映射到根据您的视图模型的出货量的集合:
In order to populate a list of shipments based on your view model object you would need to create a mapping method to map from your collection of shipments from your database to a collection of shipments based on your view model:
var model = new List<ShippingHeaderSummaryVM>();
foreach(var h in shipments)
{
var viewModel = new ShippingHeaderSummaryVM
{
ID = h.ID
Site = r.Site
DateShipped = h.DateShipped
EstDeliveryDate = h.EstDeliveryDate
TrackingNo = h.TrackingNumber
FromSitePOC = e.LastName
NumOrders = h.ShippingLI.Count
Shipper = s.Shipper
HeaderComments = h.HeaderComments
}
model.Add(viewModel);
}
return model;
作为一个方面说明,这成为一个班轮你有AutoMapper后启动并运行:
As a side note, this becomes a one liner after you have AutoMapper up and running:
var model = Mapper.Map<IEnumerable<ShippingHdr>, IEnumerable<ShippingHeaderSummaryVM>>(shipments);
虽然,学习如何手工做的事情是伟大的。手动映射模型以任何方式或形式并没有真正对你有益。去AutoMapper。
While, learning how to do things manually is great. Manually mapping models doesn't really benefit you in any way or form. Go with AutoMapper.
这篇关于如何手动填充视图模型(不使用AutoMapper!)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!