简体   繁体   English

列出来自两个不同模型的值

[英]List values from two different models

I want to create a foreach that contains List data from two different tables (models), but I have this error message: 我想创建一个包含来自两个不同表(模型)的列表数据的foreach,但是我收到此错误消息:

Controller: 控制器:

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);

Service: 服务:

 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;
    }

ViewModel: 视图模型:

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; }


    }
 }

What am I doing wrong? 我究竟做错了什么?

Help is very appreciated, thanks for reading. 非常感谢您的帮助,感谢您的阅读。

The YSOD is telling you what the problem is: you don't have a default constructor on the controller, and the DefaultControllerFactory wants one. YSOD告诉您问题出在哪里:控制器上没有默认的构造函数,而DefaultControllerFactory需要一个默认的构造函数。

The minimum required to get your code working is to add the requisite default constructor. 使代码正常工作所需的最低要求是添加必需的默认构造函数。 You can chain into the overloaded constructor, like so: 您可以链接到重载的构造函数中,如下所示:

public class ProductosController : BasicController
{
    private readonly ProductosService ProductosService;

    public ProductosController()
        : this(new ProductosService())
    {

    }

    public ProductosController(ProductosService productosService)
    {
        ProductosService = productosService;
    }
}

If you are using a DI like Unity, you need to make sure that you are registering your Interface classes to Map with your repositories: 如果您使用的是像Unity这样的DI,则需要确保将接口类注册为使用存储库映射:

eg: In your Service: 例如:在您的服务中:

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;
    }

I would create a IProductosService interface which is what your controller consumes: 我将创建一个IProductosService接口,这是您的控制器使用的接口:

   private readonly IProductosService _productosService

   public ProductosController(IProductosService productosService)
    {
        _productosService = productosService;
    }

The main problem you are having is because your BaseService has a constructor, if you remove this constructor then you will be able to have the BaseService nicely be created by the DI. 您遇到的主要问题是因为BaseService具有构造函数,如果删除此构造函数,则可以使DI很好地创建BaseService。

If you are using something like Unity to do your DI Configuration, you need to register the Mapping between Concrete and Abstract Classes: 如果使用Unity之类的工具来进行DI配置,则需要注册具体类和抽象类之间的映射:

public static void RegisterTypes(IUnityContainer container)

            {

                container.RegisterType<IProductosService, ProductosService>(new PerRequestLifetimeManager());
                Synergy5.ComponentConfigure.UnityConfigure.RegisterTypes(container, ()
    => new PerRequestLifetimeManager());

            }

This is because when the Compiler comes across your Controller with a type of ProductosService, it doesnt know how to instantiate it, because there is a constructor on it where it needs to try to push a value in, yet that value is going to be null cause it does not actually know where to get it from. 这是因为当编译器遇到带有ProductosService类型的Controller时,它不知道如何实例化它,因为它上面有一个构造函数,需要在该构造函数中尝试推入一个值,但该值将为null因为它实际上并不知道从哪里获得它。

My rule which seems to work, is that you should always use an interface to get a instance of any of your Repositories or Services, not the Concrete object, as you need to instantiate a concrete object, you don't need to instantiate the constructor of an Abstract class. 我的似乎有效的规则是,您应该始终使用接口来获取任何存储库或服务的实例,而不是具体对象,因为您需要实例化具体对象,而无需实例化构造函数。抽象类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM