Reputation: 148
I get an error trying to pass a IQueryable to a view that needs an IEnumerable. I either need to convert the IQueryable to an IEnumerable or change my view to accept and use the IQueryable. What I don't get is if I get the contents both a table and a query are lists. It shouldn't be that difficult to work with.
Here is my controller:
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>
Here is the model (just put the first few fields for clarity)
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; }
Here is the full view!
@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>
I've added the Biredat2 class here ( Just the top portion! )
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; }
Upvotes: 1
Views: 2935
Reputation: 43870
you have to fix the action
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);
}
I left empty properties, because you didn't publish any classes.
and by the way it would be better if you change your model to list too
@model List<OASIS_MVC.Models.AnalysteMain>
you will need it soon as far I can see it now already. Fix the view
@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>
........
Upvotes: 1