Ph

Ph

  • NA
  • 42
  • 1.1k

how to viewmodel with services in mvc

Jan 10 2019 5:40 PM
hI,
I have a application where I need to display the products using view model and service layer but I dont know how to connect both
  1. public class Product   
  2. {   
  3.     public int ID { getset; }   
  4.     public string Prod_SKU { getset; }   
  5.     public string Prod_Name { getset; }   
  6.     public double Price { getset; }   
  7. }  
  8. public class ProductService : IProductService   
  9. {   
  10.     private ProductContext _context; public ProductService(ProductContext context)   
  11.     {   
  12.         _context = context;   
  13.     }   
  14.     public IEnumerable<Product> GetAll()   
  15.     {   
  16.         return _context.Products.ToList();   
  17.     }   
  18. }  
product view model
  1. public class ProductViewModel   
  2. {   
  3.     public int ProductId { getset; }   
  4.     public string ProductSku { getset; }   
  5.     public string ProductName { getset; }   
  6.     public double ProductPrice { getset; }   
  7.     public List<Product> Products { getset; }   
  8. }  
now in action I have written like this
  1. private IProductService _productService;   
  2. private ProductViewModel _productViewModel;   
  3. public ProductController(IProductService productService, ProductViewModel productViewModel)   
  4. {   
  5.     _productService = productService;   
  6.     _productViewModel = productViewModel;   
  7. }   
  8. // GET: Product   
  9. public ActionResult Index()   
  10. {   
  11.     _productService.GetAll();   
  12.     return View(_productService.GetAll());   
  13. }   
  14. }  
now I want to connect all my viewmodel with service class but dono how can u give some help on using extension methods in thsi example

Answers (1)