应用场景: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,
};
}
时间: 2024-10-06 02:47:05