【问题标题】:The model item passed into the ViewDataDictionary is of type 'Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable传递到 ViewDataDictionary 的模型项的类型为“Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable”
【发布时间】:2021-09-21 17:06:01
【问题描述】:

尝试将 IQueryable 传递给需要 IEnumerable 的视图时出现错误。我要么需要将 IQueryable 转换为 IEnumerable,要么更改我的视图以接受并使用 IQueryable。 我没有得到的是,如果我得到一个表和一个查询的内容都是列表。使用起来应该没有那么难。

这是我的控制器:

    namespace OASIS_MVC.Controllers
{
    public class MainsController : Controller
    {
        private readonly ApplicationDbContext _db3;
        public MainsController(ApplicationDbContext db3)
        {
            _db3 = db3;

        }

        public IActionResult Index()
        {
            IEnumerable<Biredat2> objList = (from m in _db3.biredat2
                                             join s in _db3.suivi on m.Reseau equals s.Reseau
                                             join sa in _db3.Sub_action on m.Reseau equals sa.reseau into _sa
                                             from x in _sa.DefaultIfEmpty()
                                             select m);

            return NewMethod(objList);
        }

        private IActionResult NewMethod(IEnumerable<Biredat2> objList)
        {
            return View(objList);
        }
    }

    // GET: Biredats
  
}


Here is the view (just put the first field for clarity)

    @model IEnumerable<OASIS_MVC.Models.AnalysteMain>

@{
    ViewData["Title"] = "Anal_Main";
}

<h1>Anal_Main</h1>

<p>
    <a asp-action="Create">Create New</a>
</p>
<table class="table">
    <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.Envelope)
        </th>

这是模型(为了清楚起见,只放前几个字段)

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

#nullable disable

namespace OASIS_MVC.Models
{
    public partial class AnalysteMain
    {
        [Key]
        public int Reseau { get; set; }
        public string Envelope { get; set; }
        public string Nom_Envelope { get; set; }
        public string Bam { get; set; }

这是全图!

@model List<OASIS_MVC.Models.AnalysteMain>

@{
    ViewData["Title"] = "Anal_Main";
}

<h1>Anal_Main</h1>

<p>
    <a asp-action="Create">Create New</a>
</p>
<table class="table">
    <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.Reseau)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Envelope)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Nom_Envelope)
            </th>

我在这里添加了 Biredat2 类(只是顶部!)

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

#nullable disable

namespace OASIS_MVC.Models
{
    public partial class Biredat2
    {
        public int Id { get; set; }
        [Key]
        public int Reseau { get; set; }
        public string Envelope { get; set; }
        public string Nom_Envelope { get; set; } 

【问题讨论】:

  • 这里是完整的错误代码! InvalidOperationException:传递到 ViewDataDictionary 的模型项的类型为“Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable1[OASIS_MVC.Models.Biredat2]', but this ViewDataDictionary instance requires a model item of type 'System.Collections.Generic.IEnumerable1[OASIS_MVC.Models.AnalysteMain]”。
  • 我看到 DATA 中的数据很好,并且包含我在联接中想要的内容。我只是不知道如何将信息作为可枚举的对象传递给视图。困扰我的另一件事是错误代码 [OASIS_MVC.Models.Biredat2] 中的这一行,因为它是从选择 m 中提取的。

标签: c# asp.net asp.net-mvc .net-core entity-framework-core


【解决方案1】:

你必须修正这个动作

 public IActionResult Index()
        {
var objList = (from m in _db3.biredat2
  join s in _db3.suivi on m.Reseau equals s.Reseau
 join sa in _db3.Sub_action on m.Reseau equals sa.reseau into _sa
  from sa in _sa.DefaultIfEmpty()
    select new AnalysteMain {
    Reseau = s.Reseau,
    Envelope =
    Nom_Envelope =
    Bam =
 }

  ).ToList();

  return view(objList);
        }

我留下了空属性,因为你没有发布任何类。

顺便说一句,如果你把你的模型也改成列表会更好

 @model List<OASIS_MVC.Models.AnalysteMain>

我现在已经看到了,您很快就会需要它。修复视图

@if(Model!=null && Model.Count>0)
{
table class="table">
    <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model[0].Reseau)
            </th>
            <th>
                @Html.DisplayNameFor(model => model[0].Envelope)
        </th>
          ........ 

【讨论】:

  • 做了更改,但现在我得到了另一种类型的错误。 “List”不包含“Reseau”的定义,并且找不到接受“List”类型的第一个参数的可访问扩展方法“Reseau”(您是否缺少 using 指令或程序集引用? ) OASIS_MVC C:\programming\Oasis\OASIS_MVC\Views\Mains\Index.cshtml 16 活动
  • 我必须查看整个索引视图
  • 它太大了,放不下。我将通过消除字段来减少它,但保留逻辑。
  • 只显示给出例外的行
  • Reseau ,Envelope 和 Nom_Envelope 都有红线....... @model List @{ ViewData["Title"] = "Anal_Main"; }

    Anal_Main

    新建

    @ Html.DisplayNameFor(model => model.Reseau) @Html.DisplayNameFor(model => model.Envelope) @Html.DisplayNameFor(model => model.Nom_Envelope)
猜你喜欢
  • 1970-01-01
  • 2020-01-12
  • 2022-01-17
  • 2020-05-13
  • 1970-01-01
  • 2020-10-20
  • 2020-05-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多