我想创建一个包含来自两个不同表(模型)的List数据的foreach,但是我有这样的错误消息:
控制器:
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using this.Models;
using this.Repositories;
using this.Services;
namespace this.Controllers
{
public class ProductosController : BasicController
{
//private EfDatabase db = new EfDatabase();
private readonly ProductosService ProductosService;
public ProductosController(ProductosService productosService)
{
ProductosService = productosService;
}
// GET: Productos
public ActionResult Productos()
{
var productos = ProductosService.GetAllProducts();
var model = new ProductosViewModel
{
ProductosListes = productos
};
return View(model);
服务:
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using this.Models;
using this.Repositories;
namespace this.Services
{
public class ProductosService : BaseService
{
public ProductosService(Repository repository)
: base(repository)
{
}
public List<ProductosModel> GetAllProducts()
{
var items = Repository.Productos()
.Include(x => x.Subcategoria)
.ToList();
return items;
}
视图模型:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace this.Models
{
public class ProductosViewModel
{
public List<ProductosModel> ProductosListes { set; get; }
public List<SubcategoriasModel> SubcategoriasListes { get; set; }
}
}
我做错了什么?
非常感谢帮助,感谢阅读。
答案 0 :(得分:1)
YSOD告诉你问题是什么:控制器上没有默认构造函数,DefaultControllerFactory需要一个。
使代码工作所需的最低要求是添加必需的默认构造函数。你可以链接到重载的构造函数,如下所示:
public class ProductosController : BasicController
{
private readonly ProductosService ProductosService;
public ProductosController()
: this(new ProductosService())
{
}
public ProductosController(ProductosService productosService)
{
ProductosService = productosService;
}
}
答案 1 :(得分:0)
如果你使用像Unity这样的DI,你需要确保你正在使用你的存储库注册你的接口类来映射:
例如:在您的服务中:
Product car = new Product();
car.Components.Add(new Engine());
car.Components.Add(new Wheel());
car.Components.Add(new Wheel());
car.Components.Add(new Wheel());
car.Components.Add(new Wheel());
我会创建一个IProductosService接口,这是控制器使用的接口:
using System; using System.Collections.Generic; using System.Data.Entity; using System.Linq; using System.Text; using System.Threading.Tasks; using DismedHmo.Models; using DismedHmo.Repositories;
namespace DismedHmo.Services { public class ProductosService : BaseService, IProductosService
{
public ProductosService(Repository repository)
: base(repository)
{
}
public List<ProductosModel> GetAllProducts()
{
var items = Repository.Productos()
.Include(x => x.Subcategoria)
.ToList();
return items;
}
您遇到的主要问题是因为您的BaseService有一个构造函数,如果您删除此构造函数,那么您将能够很好地由DI创建BaseService。
如果您使用Unity之类的东西进行DI配置,则需要注册混凝土和抽象类之间的映射:
private readonly IProductosService _productosService
public ProductosController(IProductosService productosService)
{
_productosService = productosService;
}
这是因为当编译器遇到一个类型为ProductosService的Controller时,它不知道如何实例化它,因为它上面有一个构造函数,它需要尝试推送一个值,但该值是为null因为它实际上并不知道从哪里获取它。
我的规则似乎有效,是你应该总是使用一个接口来获取任何存储库或服务的实例,而不是Concrete对象,因为你需要实例化一个具体的对象,你不需要实例化Abstract类的构造函数。