标签:
应用场景:MVC中 从业务逻辑层得到product 列表后,用viewModel 展示到view中
public class TestController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult Index()
{
//从serviece 得到 product 的list
var products = _productService.getProducts();
//c=>c.ToViewModel()); 从product -> productViewModel
var productViewModels = products.ConvertAll(c=>c.ToViewModel());
//返回productviewModel
return View(productViewModels);
}
}
ToViewModel 的方法如下:扩展方法
public static ProductViewModel ToViewModel(this Product product)
{
return new ProductViewModel
{
Id = product.Id,
Name = product.Name,
};
}
MVC中Model 转ViewModel ConvertAll 方法
标签:
原文地址:http://www.cnblogs.com/angyangzhanfang/p/5459332.html