【发布时间】:2020-07-09 05:35:55
【问题描述】:
传递到 ViewDataDictionary 的模型项的类型为“System.Collections.Generic.List1[website.Models.main]', but this ViewDataDictionary instance requires a model item of type 'System.Collections.Generic.List1[website.Models.List]”
这里我使用 EF 包含方法加入了四个表。这个错误发生在我执行这个方法时。
控制器:
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";
}
模型:- 我不知道我在这里做错了什么,请给我任何解决方案
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; }
}
}
【问题讨论】:
标签: c# asp.net .net asp.net-mvc asp.net-core