【问题标题】:Entity ,Model ,ViewModel and send list to View实体、模型、视图模型并将列表发送到视图
【发布时间】:2023-02-14 11:15:19
【问题描述】:

抱歉,这将是一个很长的问题。但有必要阐明其中的逻辑。

该实体具有数据库中的所有列:

public partial class Institution
{
    public int? Id { get; set; }

    public string? District { get; set; }
    public string? InstitutionCode { get; set; }
    public string? InstitutionName { get; set; }
    public string? DemolitionStatus { get; set; }
    public string? ReinforcementStatus { get; set; }
    public string? BuildingOwnerStatus { get; set; }
    public string? BuildingOwnerInstitution { get; set; }
    public string? ClosureStatus { get; set; }
    public string? ActivityStatus { get; set; }
    public string? ETStatus { get; set; }
    public int? ETPhase1 { get; set; }
    public int? ETPhase2 { get; set; }
    public int? ETPhase3 { get; set; }
    public string? InfrastructureStatus { get; set; }
    public string? InfrastructureScope { get; set; }
    public string? InfrastructureInfo { get; set; }
    public string? InfrastructureScopeOut { get; set; }
    public string? IAccessStatus { get; set; }
    public string? IAccessType { get; set; }
    public string? ComputerClassStatus { get; set; }
    public int? ComputerClassNumber { get; set; }
    public int? PCNumber { get; set; }
    public string? ComputerClassScope { get; set; }
    public int? ETNeed { get; set; }
}

我已经为包含 ET 的列定义了一个模型,因此不会处理所有列,因为我将只显示与 ET 相关的用户数据。

public class ETModel
{
    public int? Id { get; set; }

    public string? District { get; set; }
    public string? InstitutionCode { get; set; }
    public string? InstitutionName { get; set; }
    public string? ActivityStatus { get; set; }
    public string? ETStatus { get; set; }

    public int? ETPhase1 { get; set; }
    public int? ETPhase2 { get; set; }
    public int? ETPhase3 { get; set; }
    public int? ETNeed { get; set; }
}

然后,我定义了一个包含列表类型成员的视图模型,因为我将向用户返回两个列表。我将此视图模型编辑为 ETmodel。

public class ETListVM
{
    public List<ETModel> ETyes { get; set; } 
    public List<ETModel> ETnone { get; set;}
}

我在控制器中实例化了视图模型。我定义了两个列表类型的变量。但我无法填写这些列表。我可以用 Institutions 实体填充它,但这次我偏离了我的目的。我正在使用所有列。我的目标是使用 ETModel 使用更少的资源。

 public IActionResult Index(string district)
 {
      ETListVM vm= new ETListVM();
      var ETyesList = c.**XXX**
                       .Where(p => p.District == district && p.ETStatus == "yes")
                       .ToList();
      var ETnoneList = c.**XXX**
                        .Where(p => p.District == district && p.ETStatus == "none")
                        .ToList();

      vm.ETyes = ETyesList;
      vm.ETnone = ETnoneList;
     
      return View();
}

如果我将 Institutions 实体写在我指定的位置XXX,它有效,但它不接受我想使用的 ETModel。感谢您的耐心等待和帮助。

注意:它只有在我定义一个具有相关属性的新实体时才有效,但此时 ETModel 变得毫无意义。

【问题讨论】:

    标签: asp.net-mvc asp.net-core


    【解决方案1】:

    不能直接匹配两个不同的模型,需要配置映射。

    例如,您可以使用AutoMapper:

    首先,安装 AutoMapper.Extensions.Microsoft.DependencyInjection NuGet 包并注册它:

    builder.Services.AddAutoMapper(typeof(Program));
    

    然后,创建一个继承自Profile的类来确定映射关系:

    public class UserProfile: Profile
    {
        public UserProfile()
        {
            CreateMap<Institution, ETModel>();
        }
    }
    

    在控制器中:

    public IActionResult Index(string district)
    {
        ETListVM vm = new ETListVM();
        var ETyesList = _context.Institution.Where(p => p.District == district && p.ETStatus == "yes").ToList();
        var ETnoneList = _context.Institution.Where(p => p.District == district && p.ETStatus == "none").ToList();
    
        List<ETModel> ETyes = new List<ETModel>();
        ETyes = _mapper.Map<List<ETModel>>(ETyesList);
    
        List<ETModel> ETnone = new List<ETModel>();
        ETnone = _mapper.Map<List<ETModel>>(ETnoneList);
    
        vm.ETyes = ETyes;
        vm.ETnone = ETnone;
    
        return View();
    }
    

    【讨论】:

      猜你喜欢
      • 2013-04-05
      • 2014-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-07
      • 2012-07-03
      • 2011-01-01
      • 1970-01-01
      相关资源
      最近更新 更多