EF4 和 MVC3 将 ViewModel 映射到实体
我有一个 EF4 实体工作组。以下是该模型的元数据供参考。
[MetadataType(typeof(WorkgroupMetaData))]
public partial class Workgroup {
public Contact manager { get; set; }
}
[Bind(Exclude = "id")]
public class WorkgroupMetaData
{
[ScaffoldColumn(false)]
public int id { get; set; }
[DisplayName("Org. Number")]
[Required(ErrorMessage = "Org. Number is required.")]
public string org_number { get; set; }
[DisplayName("Workgroup Name")]
[Required(ErrorMessage = "Workgroup name is required.")]
public string name { get; set; }
[DisplayName("Customer Contact")]
public int customer_contact_id { get; set; }
[DisplayName("Manager")]
public int manager_id { get; set; }
[DisplayName("Tech. Lead")]
public int lead_id { get; set; }
[DisplayName("Time Approver")]
public int time_approver { get; set; }
[DisplayName("Description")]
public string description { get; set; }
[ScaffoldColumn(false)]
public object created_at { get; set; }
[ScaffoldColumn(false)]
public object last_modified_at { get; set; }
}
我有一个 ViewModel 定义为:
public class WorkgroupViewModel
{
public Workgroup Workgroup { get; set; }
public List<Workgroup> Workgroups { get; set; }
}
在视图上我有一个网格来转储可用的工作组。这可行,但我想知道如何将 ID 字段转换为另一个表中的实际字符串。基本上,经理、customer_contact、lead 都是对联系人实体的引用。我想显示联系人中的姓名,而不仅仅是 ID。
如何才能做到这一点?我环顾四周,但似乎找不到建议或答案。也许我从错误的角度看待这个问题?
I have an EF4 Entity Workgroup. Below is the meta-data for that model for reference.
[MetadataType(typeof(WorkgroupMetaData))]
public partial class Workgroup {
public Contact manager { get; set; }
}
[Bind(Exclude = "id")]
public class WorkgroupMetaData
{
[ScaffoldColumn(false)]
public int id { get; set; }
[DisplayName("Org. Number")]
[Required(ErrorMessage = "Org. Number is required.")]
public string org_number { get; set; }
[DisplayName("Workgroup Name")]
[Required(ErrorMessage = "Workgroup name is required.")]
public string name { get; set; }
[DisplayName("Customer Contact")]
public int customer_contact_id { get; set; }
[DisplayName("Manager")]
public int manager_id { get; set; }
[DisplayName("Tech. Lead")]
public int lead_id { get; set; }
[DisplayName("Time Approver")]
public int time_approver { get; set; }
[DisplayName("Description")]
public string description { get; set; }
[ScaffoldColumn(false)]
public object created_at { get; set; }
[ScaffoldColumn(false)]
public object last_modified_at { get; set; }
}
I've got a ViewModel defined as:
public class WorkgroupViewModel
{
public Workgroup Workgroup { get; set; }
public List<Workgroup> Workgroups { get; set; }
}
On the view I have a grid to dump out the workgroups available. This works but I was wondering how to convert the ID fields to the actual strings from another table. Basically the manager, customer_contact, lead are all references to the Contact entity. I would like to show the names from Contacts instead of just the id.
How can this be accomplished? I've looked around a bit but I can't seem to find a suggestion or an answer. Maybe I looking at this from the wrong perspective?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能会考虑使用工作组(装饰器模式)或元组的包装器或创建将它们绑定在一起的自定义类。
在 EF 查询中,您可以执行以下操作:
然后您的视图模型可以具有:
或
You might consider using a wrapper around Workgroup (decorator pattern) or Tuple or creating a custom class that binds them together.
In your EF query you can do something like:
Then your view model could have:
or