Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
.NET
.NET Core
.NET MAUI
.NET Standard
Active Directory
ADO.NET
Agile Development
AI
AJAX
AlbertAGPT
Alchemy
Alexa Skills
Algorand
Algorithms in C#
Android
Angular
ArcObject
ASP.NET
ASP.NET Core
Augmented Reality
Avalanche
AWS
Azure
Backbonejs
Big Data
BizTalk Server
Blazor
Blockchain
Bootstrap
Bot Framework
Business
Business Intelligence(BI)
C#
C# Corner
C# Strings
C, C++, MFC
Career Advice
Careers and Jobs
Chapters
ChatGPT
Cloud
Coding Best Practices
Cognitive Services
COM Interop
Compact Framework
Copilot
Cortana Development
Cosmos DB
Cryptocurrency
Cryptography
Crystal Reports
CSS
Current Affairs
Custom Controls
Cyber Security
Data Mining
Data Science
Databases & DBA
Databricks
Design Patterns & Practices
DevExpress
DevOps
DirectX
Dynamics CRM
Enterprise Development
Entity Framework
Error Zone
Exception Handling
F#
Files, Directory, IO
Flutter
Games Programming
GDI+
General
Generative AI
GO
Google Cloud
Google Development
Graphics Design
Graphite Studio
Hardware
Hiring and Recruitment
HoloLens
How do I
HTML 5
Infragistics
Internet & Web
Internet of Things
Ionic
Java
Java and .NET
JavaScript
JQuery
JSON
JSP
Knockout
Kotlin
Langchain
Leadership
Learn .NET
Learn iOS Programming
LINQ
Machine Learning
Metaverse
Microsoft 365
Microsoft Fabric
Microsoft Office
Microsoft Phone
Microsoft Teams
Mobile Development
MongoDB
MuleSoft
MySQL
NEAR
NetBeans
Networking
NFT
NoCode LowCode
Node.js
Office Development
OOP/OOD
Open Source
Operating Systems
Oracle
Outsourcing
Philosophy
PHP
Polygon
PostgreSQL
Power Apps
Power Automate
Power BI
Power Pages
Printing in C#
Products
Progress
Progressive Web Apps
Project Management
Public Speaking
Python
Q#
QlikView
Quantum Computing
R
React
React Native
Reports using C#
Robotics & Hardware
RPA
Ruby on Rails
RUST
Salesforce
Security
Servers
ServiceNow
SharePoint
Sharp Economy
SignalR
Smart Devices
Snowflake
Software Architecture/Engineering
Software Testing
Solana
Solidity
Sports
SQL
SQL Server
Startups
Stratis Blockchain
Swift
SyncFusion
Threading
Tools
TypeScript
Unity
UWP
Visual Basic .NET
Visual Studio
Vue.js
WCF
Wearables
Web API
Web Design
Web Development
Web3
Windows
Windows Controls
Windows Forms
Windows PowerShell
Windows Services
Workflow Foundation
WPF
Xamarin
XAML
XML
XNA
XSharp
Register
Login
4
Answers
Cannot convert list to Ienumerable in mvc
Mark Tabor
5y
6.7k
1
Reply
Hi i have mvc web application in which i am stroing courses and students into the table like below
ID
student_Id
course_Id
1
1
2
2
1
1
3
2
1
Now in my database table i am saving separate rows for each course & students like student A is having 2 courses and student B is having 1 course as shown above.
Right now My index view is shows three rows what i need to show 2 rows like i need 1 row for each students having comma separated courses below are my model class
public
class
StudentCoursesViewModel
{
public
Moduel Modules {
get
;
set
; }
public
Years Year {
get
;
set
; }
public
Student studnets {
get
;
set
; }
public
Programs programs {
get
;
set
; }
public
IEnumerable<Courses> curses {
get
;
set
; }
public
Courses courses {
get
;
set
; }
public
Student_Assigned_courses studentAssignedCourses {
get
;
set
; }
}
}
My controller :
using
(DatabaseContext db =
new
DatabaseContext())
{
List<Student_Assigned_courses> Student_Assigned_courses = db.StudentCoursesAssigned.ToList();
List<Courses> courses = db.Courses.ToList();
List<Moduel> Module = db.Moduels.ToList();
List<Years> year = db.Years.ToList();
List<Programs> Programs = db.Programs.ToList();
List<Student> students = db.Students.ToList();
List<Courses> curr = db.Courses.ToList();
var employeeRecord = from e
in
Student_Assigned_courses
join d
in
students on e.Student_id equals d.Student_Id into table1
from d
in
table1.ToList()
join h
in
courses on e.Course_Id equals h.Course_Id into table6
from h
in
table6.ToList()
join i
in
courses on e.Course_Id equals i.Course_Id into table2
from i
in
table2.ToList()
join m
in
Module on e.Module_Id equals m.Id into table3
from m
in
table3.ToList()
join y
in
year on e.Year_Id equals y.Id into table4
from y
in
table4.ToList()
join p
in
Programs on e.Program_Id equals p.Id into table5
from p
in
table5.ToList()
select
new
StudentCoursesViewModel
{
studentAssignedCourses = e,
courses = i,
programs = p,
curses=h,
studnets = d,
Year = y,
Modules = m,
};
return
View(employeeRecord);
}
My view:
@model IEnumerable<CMS_Monitoring.ViewModels.StudentCoursesViewModel>
@{
ViewBag.Title =
"Index"
;
Layout =
"~/Views/Shared/_Layout2.cshtml"
;
}
<h2>Index</h2>
<p>
@Html.ActionLink(
"Create New"
,
"Create"
)
</p>
<table
class
=
"table"
>
<tr>
<th>
@Html.DisplayNameFor(model => model.studnets.Student_FName)
</th>
<th>
@Html.DisplayNameFor(model => model.courses.Course_Name)
</th>
<th>
@Html.DisplayNameFor(model => model.courses.Status)
</th>
<th>
@Html.DisplayNameFor(model => model.studentAssignedCourses.Date_Created)
</th>
@*<th>
@Html.DisplayNameFor(model => model.studentAssignedCourses.Date_Modified)
</th>*@
<th>
@Html.DisplayNameFor(model => model.studentAssignedCourses.Assigned_By)
</th>
<th>
@Html.DisplayNameFor(model => model.Modules.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.programs.Program_Title)
</th>
<th>
@Html.DisplayNameFor(model => model.Year.Name)
</th>
<th>Actions</th>
</tr>
@
foreach
(var item
in
Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.studnets.Student_FName)
</td>
<td>
@Html.DisplayFor(modelItem => item.courses.Course_Name)
</td>
<td>
@{
foreach
(var course
in
item.curses)
{
@course.Course_Code @: @course.Course_Name <br />
}
}
</td>
<td>
@Html.DisplayFor(modelItem => item.courses.Status)
</td>
<td>
@Html.DisplayFor(modelItem => item.studentAssignedCourses.Date_Created)
</td>
@*<td>
@Html.DisplayFor(modelItem => item.studentAssignedCourses.Date_Modified)
</td>*@
<td>
@Html.DisplayFor(modelItem => item.studentAssignedCourses.Assigned_By)
</td>
<td>
@Html.DisplayFor(modelItem => item.Modules.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.programs.Program_Title)
</td>
<td>
@Html.DisplayFor(modelItem => item.Year.Name)
</td>
<td>
@Html.ActionLink(
"Edit"
,
"Edit"
,
new
{ id = item.studentAssignedCourses.Id }) |
@Html.ActionLink(
"Details"
,
"Details"
,
new
{ id = item.studentAssignedCourses.Id }) |
@Html.ActionLink(
"Delete"
,
"Delete"
,
new
{ id = item.studentAssignedCourses.Id })
</td>
</tr>
}
</table>
Below error screen
Post
Reset
Cancel
Answers (
4
)
Next Recommended Forum
Disable chunking in Asp.Net Core 3.1
Implement calendar scheduler in MVC 3