【问题标题】:The model item passed into the ViewDataDictionary is of type 'System.Collections.Generic.List`传递到 ViewDataDictionary 的模型项的类型为“System.Collections.Generic.List”
【发布时间】: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


    【解决方案1】:

    尝试将项目解析为您自己的 List 对象

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

    【讨论】:

    • 。它工作但在视图中我无法获得我需要的正确数据..我可以更改视图中的任何内容吗??
    • 你需要循环列表如下@foreach(var item in Model){ //print items }
    • 谢谢,我是我的朋友,你是我的朋友。萨卢多斯
    【解决方案2】:

    listAllmain对象的列表,你需要在你的razor页面中使用对应的类型来接受它:

    @model List<website.Models.main>
    
    @{
        ViewData["Title"] = "Index"; 
    }
    

    【讨论】:

      猜你喜欢
      • 2021-09-21
      • 2020-01-12
      • 2022-01-17
      • 2020-05-13
      • 1970-01-01
      • 2020-10-20
      • 2020-05-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多