Home > database >  Entity ,Model ,ViewModel and send list to View
Entity ,Model ,ViewModel and send list to View

Time:12-27

This will be a long question, sorry. But it was necessary to unravel the logic.

This Entity has all colunms in DB:

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

I have defined a model for columns containing ET so that all columns are not processed since I will only show the user data related to 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; }
}

Then, I defined a viewmodel with a member of the list type, as I will return two lists to the user. I edited this viewmodel to ETmodel.

 public class ETListVM
{

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

}

I instantiated the viewmodel in the controller. I have defined two variables of list type. But I'm having trouble filling out these lists. I can fill it with the Institutions entity, but this time I'm getting away from my purpose. I am using all columns. My goal is to use less resources using the 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();
    }

If I write the Institutions entity where I specified as XXX, it works, but it does not accept the ETModel I want to use. Thank you for your patience and help. Note: It only works if I define a new entity with related properties, but this time ETModel becomes meaningless.

CodePudding user response:

You can't directly match two different models, you need to configure the mapping.

For example, you can use AutoMapper:

First, Install AutoMapper.Extensions.Microsoft.DependencyInjection NuGet Package and register it:

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

Then, Create a class inherited from Profile to determine the mapping relationship:

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

And in controller:

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();
}
  • Related