C# 传递到ViewDataDictionary的模型项的类型为';System.Collections.Generic.List`

C# 传递到ViewDataDictionary的模型项的类型为';System.Collections.Generic.List`,c#,asp.net,.net,asp.net-mvc,asp.net-core,C#,Asp.net,.net,Asp.net Mvc,Asp.net Core,传递到ViewDataDictionary的模型项的类型为“System.Collections.Generic.List1[website.Models.main]”,但此ViewDataDictionary实例需要类型为“System.Collections.Generic.List1[website.Models.List]”的模型项 在这里,我使用EF include方法连接了四个表。此错误发生在我使用此方法时 控制器: public IActionResult Index()

传递到ViewDataDictionary的模型项的类型为“System.Collections.Generic.List
1[website.Models.main]”,但此ViewDataDictionary实例需要类型为“System.Collections.Generic.List
1[website.Models.List]”的模型项

在这里,我使用EF include方法连接了四个表。此错误发生在我使用此方法时

控制器:

public IActionResult Index()
        {
            
            var listAll = db.main
                .Include(x => x.Person)
                .ThenInclude(x => x.Entity)
                .ThenInclude(x => x.Country)            
                .ToList();
return View(listAll);

        }
视图:-

 @model List<website.Models.List>

@{
    ViewData["Title"] = "Index"; 
    }
@型号列表
@{
ViewData[“标题”]=“索引”;
}
模特:-我不知道我做错了什么,请给我任何解决方案

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Collections;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace website.Models
{
    public class List

    {
        public mainmain{ get; set; }
        public Persons Person { get; set; }
        public other other{ get; set; }
        public Entities Entity { get; set; }
        public Countries Country { get; set; }
        public int countryId { get; internal set; }
    }

    public class main
    {
        public int id{ get; set; }
        public int TypeId { get; set; }
        public int? PersonId { get; set; }
    
    }

    public class Person
    {
        public Person()
        {
            main= new HashSet<main>();
        }
        public int PersonId { get; set; }
        public string FirstNameEn { get; set; }
        public string FirstNameAr { get; set; }
        public string SecondNameAr { get; set; }
        public string HomePhonePart1 { get; set; }
        public string HomePhonePart2 { get; set; }
        public ICollection<main> main{ get; set; }
    }

    public class Other
    {
        public int PersonId { get; set; }
        public string FatherReligio { get; set; }
        public bool? Fatherless { get; set; }
        public DateTime? FatherDeathDate { get; set; }
        public bool? Motherless { get; set; }
  
     
    }

    public classEntity
    {
        public Entity()
        {
            Persons = new HashSet<Persons>();
        }
       
        public int CountryId { get; set; }
        public string Name { get; set; }
        public string ResponsibleName { get; set; }
        public string Address { get; set; }
        public string Pobox { get; set; }
        public string PhonePart1 { get; set; }
        public string PhonePart2 { get; set; }
      
        public ICollection<Persons> Persons { get; set; }
    }

    public class country
    {
        public country()

        {
           Entity = new HashSet<Entities>();
            Persons = new HashSet<Persons>();
        }
       
        public string NameEn { get; set; }
        public string NameFr { get; set; }
        public string NameSp { get; set; }
        public string NameUr { get; set; }
        public virtual ICollection<Entities> Entity { get; set; }
        public ICollection<Persons> Persons { get; set; }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Threading.Tasks;
使用System.Web;
使用系统集合;
使用System.ComponentModel.DataAnnotations;
使用System.ComponentModel.DataAnnotations.Schema;
名称空间网站.模型
{
公共班级名单
{
公共main{get;set;}
公众人物{get;set;}
公共其他{get;set;}
公共实体实体{get;set;}
公共国家/地区{get;set;}
public int countryId{get;内部集合;}
}
公共班机
{
公共int id{get;set;}
公共int类型ID{get;set;}
公共int?PersonId{get;set;}
}
公共阶层人士
{
公众人士()
{
main=newhashset();
}
公共int PersonId{get;set;}
公共字符串FirstNameEn{get;set;}
公共字符串FirstNameAr{get;set;}
公共字符串SecondNameAr{get;set;}
公共字符串HomePhonePart1{get;set;}
公共字符串HomePhonePart2{get;set;}
公共ICollection main{get;set;}
}
公共类其他
{
公共int PersonId{get;set;}
公共字符串FatherReligio{get;set;}
公共布尔?无父{get;set;}
公共日期时间?父死亡日期{get;set;}
公共布尔?无母{get;set;}
}
公共类实体
{
公共实体()
{
Persons=新HashSet();
}
public int CountryId{get;set;}
公共字符串名称{get;set;}
公共字符串ResponsibleName{get;set;}
公共字符串地址{get;set;}
公共字符串Pobox{get;set;}
公共字符串PhonePart1{get;set;}
公共字符串PhonePart2{get;set;}
公共ICollection Persons{get;set;}
}
公营国家
{
公共国家()
{
实体=新的HashSet();
Persons=新HashSet();
}
公共字符串名称{get;set;}
公共字符串名称fr{get;set;}
公共字符串NameSp{get;set;}
公共字符串NameUr{get;set;}
公共虚拟ICollection实体{get;set;}
公共ICollection Persons{get;set;}
}
}

尝试将项目解析到您自己的列表对象中

public IActionResult Index()
{       
    var listAll = db.main
                    .Include(x => x.Person)
                    .ThenInclude(x => x.Entity)
                    .ThenInclude(x => x.Country)            
                    .ToList();
    
    List<website.Models.List> newList = new List<website.Models.List>();
    foreach(var item in listAll){
          website.Models.List listItem = new website.Models.List();
          listItem.countryId = item.countryId;
          //add your remaining fields
          newList.Add(listItem);
    }

    return View(newList);
}
public IActionResult Index()
{       
var listAll=db.main
.包括(x=>x人)
.然后包括(x=>x.Entity)
.然后包括(x=>x.Country)
.ToList();
List newList=新列表();
foreach(listAll中的变量项){
website.Models.List listItem=新的website.Models.List();
listItem.countryId=item.countryId;
//添加剩余字段
newList.Add(列表项);
}
返回视图(newList);
}

尝试将项目解析到您自己的列表对象中

public IActionResult Index()
{       
    var listAll = db.main
                    .Include(x => x.Person)
                    .ThenInclude(x => x.Entity)
                    .ThenInclude(x => x.Country)            
                    .ToList();
    
    List<website.Models.List> newList = new List<website.Models.List>();
    foreach(var item in listAll){
          website.Models.List listItem = new website.Models.List();
          listItem.countryId = item.countryId;
          //add your remaining fields
          newList.Add(listItem);
    }

    return View(newList);
}
public IActionResult Index()
{       
var listAll=db.main
.包括(x=>x人)
.然后包括(x=>x.Entity)
.然后包括(x=>x.Country)
.ToList();
List newList=新列表();
foreach(listAll中的变量项){
website.Models.List listItem=新的website.Models.List();
listItem.countryId=item.countryId;
//添加剩余字段
newList.Add(列表项);
}
返回视图(newList);
}

listAll是
main
对象的列表,您需要使用相应的类型在页面中访问它:

@model List<website.Models.main>

@{
    ViewData["Title"] = "Index"; 
}
@型号列表
@{
ViewData[“标题”]=“索引”;
}

listAll是
main
对象的列表,您需要使用相应的类型在页面中访问它:

@model List<website.Models.main>

@{
    ViewData["Title"] = "Index"; 
}
@型号列表
@{
ViewData[“标题”]=“索引”;
}

。它正在工作,但在视图中,我无法获得我需要的正确数据。我可以更改视图中的任何内容吗?您需要循环遍历列表,如下@foreach(模型中的var项){//print items}.It's working,但在视图中,我无法获得我需要的正确数据..我可以更改视图中的任何内容吗?您需要在列表中循环,如下@foreach(模型中的var项){//print items}