The model item passed into the ViewDataDictionary EF but this ViewDataDictionary instance requires 'System.Collections.Generic.List

huangapple go评论113阅读模式
英文:

The model item passed into the ViewDataDictionary EF but this ViewDataDictionary instance requires 'System.Collections.Generic.List

问题

要将我的ASP.NET MVC Web应用与数据库连接,使用Entity Framework Core(v5.17)。但在尝试最终使它工作时,它显示了以下消息:

> InvalidOperationException: 传递到ViewDataDictionary的模型项的类型为'Microsoft.EntityFrameworkCore.Internal.InternalDbSet1[test_deploiement.Models.PublisherModel]',但此ViewDataDictionary实例需要类型为'System.Collections.Generic.List1[test_deploiement.Models.PublisherModel]'的模型项。

模型类:

public class PublisherModel
{
    [Key]
    public int Id { get; set; }
    [Required]
    public string Name { get; set; }

    public PublisherModel(int id, string name)
    {
        Id = id;
        Name = name;
    }
}

控制器:

public class HomeController : Controller
{
    private readonly ApplicationDbContext _db;
    public IEnumerable<PublisherModel> Publishers { get; set; }

    public HomeController(ApplicationDbContext db)
    {
        _db = db;
    }

    public IActionResult adminpublishermanagement()
    {
        Publishers = _db.Publisher_master_tbl;
        return View(Publishers);
    }
}

视图:

<tbody>
@foreach (var Publishers in Model)
{
   <tr>
      <td scope="row">@Html.DisplayFor(modelItem => Publishers.Id)</td> 
      <td>@Html.DisplayFor(modelItem => Publishers.Name)</td>
  </tr>
}
</tbody>

我尝试查看我的类的名称是否有问题,但我不确定。

英文:

I want to connect my ASP.NET MVC web app with the database using Entity Framework Core (v5.17).

But when trying to make it work finally it show me this message :

> InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'Microsoft.EntityFrameworkCore.Internal.InternalDbSet1[test_deploiement.Models.PublisherModel]', but this ViewDataDictionary instance requires a model item of type 'System.Collections.Generic.List1[test_deploiement.Models.PublisherModel]'.

Model class:

public class PublisherModel
{
    [Key]
    public int Id { get; set; }
    [Required]
    public string Name { get; set; }

    public PublisherModel(int id, string name)
    {
        Id = id;
        Name = name;
    }
}

Controller

public class HomeController : Controller
{
    private readonly ApplicationDbContext _db;
    public IEnumerable<PublisherModel> Publishers { get; set; }

    public HomeController(ApplicationDbContext db)
    {
        _db = db;
    }

    public IActionResult adminpublishermanagement()
    {
        //List<PublisherModel> publisher = new List<PublisherModel>();
        //publisher.Add(new PublisherModel(101, " Lafond"));
        //publisher.Add(new PublisherModel(102, " Casio"));
        //publisher.Add(new PublisherModel(103, "J'aime lire"));
        //publisher.Add(new PublisherModel(104, "Time ofpost"));
        //publisher.Add(new PublisherModel(105, "Le quotidien"));
        Publishers = _db.Publisher_master_tbl;
        return View(Publishers);
    }
}

View

<tbody>
@foreach (var Publishers in Model)
{
   <tr>
      <td scope="row">@Html.DisplayFor(modelItem => Publishers.Id)</td> 
      <td>@Html.DisplayFor(modelItem => Publishers.Name)</td>
  </tr>
}
</tbody>

I tried to look if the name of my classes were disfunctional but I'm not sure of it.

答案1

得分: 1

你应该在将它传递给视图之前在 DbSet 上调用 .ToList():

Publishers = _db.Publisher_master_tbl.ToList();
英文:

You should call .ToList() on the DbSet before passing it to your view:

Publishers = _db.Publisher_master_tbl.ToList();

huangapple
  • 本文由 发表于 2023年3月7日 23:50:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75664225.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定