【发布时间】: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.EntityQueryable
1[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