我应该如何将编辑模型映射到域模型

发布于 2024-11-28 07:47:40 字数 1285 浏览 0 评论 0原文

我试图了解将编辑模型中的字段(从视图表单发回)映射到域模型的最佳方法,以便我可以 SubmitChanges() 并更新数据库记录(通过 Linq to SQL)。我的例子很简单,答案可能就是手动完成。我要问的问题是对于具有更多字段的模型,仍然可以手动完成,也许这就是答案 - 但有没有更简单的方法(也许使用 AutoMapper)?

我的域模型:

public class Product
{
    public int ProductId { get; set; }
    public string Name { get; set; }
    public int Color { get; set; }
    public string SerialNumber { get; set; }
}

我的视图/编辑模型:

public class ProductVM
{
    public int ProductId { get; set; }
    public string Name { get; set; }
    public IEnumberable<Color> Colors { get; set; } // To populate a dropdown in the view with color key/values
}

我的控制器操作:

[HttpPost]
public ActionResult UpdateProduct(ProductVM product)
{
    var productService = new ProductService();
    productService.Update(product);
}

我的服务层中的更新方法:

public void Update(ProductVM productVM)
{
    Product product = dataContext.Products.Single(p=>p.ProductId == productVM.ProductId);
    // What's the best way to map the fields of productVM into product so I can submit the changes?
    dataContext.SubmitChanges();
}

我只想映射 ProductVM 中与产品中的字段匹配的字段。编辑模型具有域模型中不存在的字段,并且域模型具有编辑模型中不存在的字段。我的最终目标是从编辑模型中的字段更新数据库中的字段。

谢谢

I'm trying to understand the best way to map the fields in an Edit Model (posted back from a view form) to a Domain Model so that I can SubmitChanges() and update the database record (via Linq to SQL). My example will be simple and the answer to it is probably to just do it manually. The question I'm asking is for models with many more fields where it could still be done manually and maybe that's the answer - but is there an easier way (using AutoMapper perhaps)?

My Domain Model:

public class Product
{
    public int ProductId { get; set; }
    public string Name { get; set; }
    public int Color { get; set; }
    public string SerialNumber { get; set; }
}

My View/Edit Model:

public class ProductVM
{
    public int ProductId { get; set; }
    public string Name { get; set; }
    public IEnumberable<Color> Colors { get; set; } // To populate a dropdown in the view with color key/values
}

My Controller Action:

[HttpPost]
public ActionResult UpdateProduct(ProductVM product)
{
    var productService = new ProductService();
    productService.Update(product);
}

The Update method in my service layer:

public void Update(ProductVM productVM)
{
    Product product = dataContext.Products.Single(p=>p.ProductId == productVM.ProductId);
    // What's the best way to map the fields of productVM into product so I can submit the changes?
    dataContext.SubmitChanges();
}

I just want to map the fields from productVM that match up with the fields in product. The Edit Model has fields that don't exist in the Domain Model and the Domain Model has fields that don't exist in the Edit Model. My ultimate goal is to update the fields in the database from the fields in the Edit Model.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

煮茶煮酒煮时光 2024-12-05 07:47:40

使用自动映射器

public void Update(ProductVM productVM)
{
    Product product = dataContext.Products.Single(p=>p.ProductId == productVM.ProductId);
    AutoMapper.Mapper.DynamicMap<ProductVM, Product >(productVM, product );
    dataContext.SubmitChanges();
}

Use Automapper

public void Update(ProductVM productVM)
{
    Product product = dataContext.Products.Single(p=>p.ProductId == productVM.ProductId);
    AutoMapper.Mapper.DynamicMap<ProductVM, Product >(productVM, product );
    dataContext.SubmitChanges();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文