简体   繁体   English

如何在ASP.NET MVC中将Controller对象列表转换为Controller上的viewmodel

[英]How can I convert a list of domain objects to viewmodels on the Controller in ASP.NET MVC

I'd like to know the best way of converting a list of domain object I retrieve into custom ViewModels in the controller 我想知道将我检索到的域对象列表转换为控制器中的自定义ViewModel的最佳方法

eg 例如

IList<Balls> _balls = _ballsService.GetBalls(searchCriteria);

into

IList<BallViewModels> _balls = _ballsService.GetBalls(searchCriteria);

it doesn't have to be exactly as I've outlined above ie it doesn't have to be an IList and if not accessing the service directly and instead go thru some other layer that converts the objects to viewmodels then that is ok too. 它不必像我上面概述的那样,即它不必是IList,如果不是直接访问服务,而是通过一些其他层将对象转换为viewmodels,那也没关系。

thanks 谢谢

For simple objects you could just use Linq: 对于简单的对象,您可以使用Linq:

IList<BallViewModel> _balls = _ballsService.GetBalls(searchCriteria)
    .Select(b => new BallsViewModel
                 {
                     ID = b.ID,
                     Name = b.Name,
                     // etc
                 })
    .ToList();

That can get pretty repetitive though, so you may want to give your BallViewModel class a constructor that accepts a Ball and does the work for you. 但是,这可能会非常重复,因此您可能希望为BallViewModel类提供一个接受Ball的构造函数,并为您完成工作。

Another approach is to use a library like AutoMapper to copy the properties (even the nested ones) from your domain object to your view model. 另一种方法是使用类似AutoMapper的库将属性(甚至是嵌套的属性)从域对象复制到视图模型。

Probably a bit of Linq, something along the lines of 可能有点像Linq,有点像

var ballQuery = from ball in _ballsService.GetBalls(searchCriteria)
                select new BallViewModels
                {
                    Diameter = ball.Diameter,
                    color = ball.Color,
                    ...
                }
IList<BallViewModels> _balls = ballQuery.ToList();

Either that or the question is more complicated than I think... 无论是那个或问题比我想的更复杂......

I use AutoMapper to do this all the time. 我使用AutoMapper一直这样做。 It's really flexible and has worked for me without any troubles so far. 它非常灵活,到目前为止我没有任何麻烦。

First you set up a map like during your app's initialization: 首先,在应用初始化期间设置地图:

Mapper.CreateMapping<Balls, BallViewModel>();

And whenever you need to map the objects, you would do this: 无论何时需要映射对象,都可以这样做:

Mapper.Map<IList<Balls>, IList<BallViewModel>>(_ballsService.GetBalls());

Like I said, it's very flexible and you can modify how the mapping happens for each property using a fluent API. 就像我说的那样,它非常灵活,您可以使用流畅的API修改每个属性的映射方式。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在ASP.NET MVC 4中保存多个ViewModel? - How can I save multiple ViewModels in ASP.NET MVC 4? ASP.NET MVC中的ViewModel和域模型 - ViewModels and domain models in ASP.NET MVC 如何在ASP.NET ViewModels中填充包含从视图到控制器的字符串列表的对象列表 - How to fill a list of objects that contain a list of string from view to controller in asp.net viewModels ASP.NET MVC实体,DTO,业务对象,ViewModels? 我该如何管理? - ASP.NET MVC Entities, DTO, Business objects, ViewModels? How do i manage this? 如何在ASP.NET MVC中使用ViewModels? - How to use ViewModels in ASP.NET MVC? 在Asp.net MVC中,视图模型是否可以从域模型派生? - In Asp.net MVC, is it advisable for viewmodels to derive from domain models? 如何将多个Json对象传递给ASP.net MVC控制器? - How can I pass multiple Json objects to ASP.net MVC controller? ASP.NET MVC - 我是否应该使用存储库模式将ViewModel写入数据库或首先将它们转换为模型? - ASP.NET MVC - Should I use the Repository Pattern to write ViewModels to the database or convert them to Models first? 如何在 asp.net 核心 mvc ZD7EFA19FBE7D39372FD5ADB60242 中将列表 object 转换为 Viewmodel object? - How can I convert List object to Viewmodel object in asp.net core mvc C#? ASP.Net MVC:如何使用列表 ViewData object 或从 controller 传递到视图的其他复杂对象 - ASP.Net MVC: How to use a List ViewData object or other complex objects passed from a controller to a view
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM