我是新來的MVC3(這就是爲什麼我買了一本書,這就是爲什麼我現在有這個問題!),所以道歉,如果有一個明顯的答案這個!將多個模型傳遞給使用MVC3和Ninject的視圖
我正在關注一個在MVC3中構建購物車的簡單示例。本書提倡使用Ninject進行依賴注入,而我也是新手。對於一個模型,在這種情況下,產品看起來似乎很簡單,但在此基礎上,我正努力添加第二個模型,並在顯示產品模型的相同視圖中顯示此模型。我試過使用視圖模型,但我發現所有的例子都包含幾個類到一個模型中,我不能完全弄清楚如何在我的代碼中實現它。
類:
public class Product
{
public int ProductId {get;set;}
public string Name {get;set;}
}
摘要庫:
public interface IProductRepository
{
IQueryable<Product> Products {get;}
}
類到模型數據庫關聯:
public class EFDbContext : DbContext
{
public DbSet<Product> Products {get;set;}
}
產品信息庫,它實現的抽象接口:
public class EFProductRepository : IProductRepository
{
private EFDbContext context = new EFDbContext();
public IQueryable<Product> Products
{
get {return context.Products;}
}
}
Ninject將IProductRepository綁定到ControllerFactory類中的EFProductRepository。
控制器:
public class ProductController : Controller
{
private IProductRepository repository;
public ProductController(IProductRepository productRepository)
{
repository = productRepository;
}
public ViewResult List()
{
return View(repository.Products);
}
}
我的問題是通過repository.Products的強類型視圖。如果我需要通過另一個實體,這是非常可行的,我將如何實現這一目標?
能否請你加你將如何再在視圖中使用這個? – Zapnologica 2013-07-15 20:28:37
你的服務是什麼?應該在哪裏添加? – Icet 2015-09-11 10:28:22