TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Ph
NA
42
1.2k
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
public
class
Product
{
public
int
ID {
get
;
set
; }
public
string
Prod_SKU {
get
;
set
; }
public
string
Prod_Name {
get
;
set
; }
public
double
Price {
get
;
set
; }
}
public
class
ProductService : IProductService
{
private
ProductContext _context;
public
ProductService(ProductContext context)
{
_context = context;
}
public
IEnumerable<Product> GetAll()
{
return
_context.Products.ToList();
}
}
product
view
model
public
class
ProductViewModel
{
public
int
ProductId {
get
;
set
; }
public
string
ProductSku {
get
;
set
; }
public
string
ProductName {
get
;
set
; }
public
double
ProductPrice {
get
;
set
; }
public
List<Product> Products {
get
;
set
; }
}
now in action I have written like this
private
IProductService _productService;
private
ProductViewModel _productViewModel;
public
ProductController(IProductService productService, ProductViewModel productViewModel)
{
_productService = productService;
_productViewModel = productViewModel;
}
// GET: Product
public
ActionResult Index()
{
_productService.GetAll();
return
View(_productService.GetAll());
}
}
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
Reply
Answers (
1
)
Export very large data to Excel file
How to create a object from collection after grouping