英文:
Passing (local variable)List<Courses> and showing the list at the view MVC
问题
我试图发送一个本地变量,它是一个列表,并在视图中显示它。
我一直在尝试各种方式,但似乎什么都不起作用,我有点绝望。
Controller
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data.SqlClient;
using System.Data;
using Project.Models;
using System.Configuration;
using Project.Dal;
using Project.ViewModel;
namespace Project.Controllers
{
public class StudentController : Controller
{
public ActionResult StudentCourse()
{
var id = (int)Session["ID"];
StudentInCourseDal SICD = new StudentInCourseDal();
CoursesDal CD = new CoursesDal();
List<Courses> courseinfo = new List<Courses>();
//var course = (from x in SICD.StudentInCourses where x.studentID.Equals(id) select x.CourseID).ToList();
//var courses = (from x in CD.Courses where x.CourseID.Equals(course) select x.Coursesinfo).ToList();
/* get all courses ids */
var courses =
(from x in SICD.StudentInCourses
where x.studentID.Equals(id)
select x.CourseID).ToList();
/* get each course info thats in courses */
var coursesInfo =
(from x in CD.Courses
where courses.Contains(x.CourseID)
select x).ToList();
return View("StudentCourse", coursesInfo);
}
public ActionResult StudentExams()
{
return View();
}
}
}
DAL
using Project.Models;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
namespace Project.Dal
{
public class CoursesDal : DbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Courses>().ToTable("Courses");
}
public DbSet<Courses> Courses { get; set; }
}
}
Model
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace Project.Models
{
public class Courses
{
[Key]
[Required]
public int CourseID { get; set; }
[Required]
public int LectureID { get; set; }
[Required]
public string Day { get; set; }
[Required]
public TimeSpan Hour { get; set; }
[Required]
public DateTime? moedADate { get; set; }
[Required]
public DateTime? moedBDate { get; set; }
[Required]
public string moedAClass { get; set; }
[Required]
public string moedBClass { get; set; }
}
}
CourseViewModel
using Project.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace Project.ViewModel
{
public class CoursesViewModel
{
public Courses courseName { get; set; }
public List<Courses> coursesName { get; set; }
}
}
StudentCourseView
@model Project.Models.Courses
<!DOCTYPE html>
<style type="text/css">
.table1 {
width: 500px;
height: 200px;
text-align: center;
}
.cell {
border: solid 1px #999;
}
.cell-1 {
border: solid 1px #999;
background: #999;
color: #FFF;
}
</style>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>StudentCourse</title>
<link href="~/styles/font-awesome.min.css" rel="stylesheet" />
<link href="~/styles/style.css" rel="stylesheet" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link href="~/styles/navbar.css" rel="stylesheet" />
</head>
<body>
<div>
<div class="navbar">
<a href="/Account/StudentHomePage" class="Button">Home</a>
<a href="/Student/StudentSchedule" class="Button">Schedule</a>
<div class="dropdown">
<button class="dropbtn">
More
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content">
<a href="/Student/StudentExams" class="Button">Exams</a>
<a href="/Student/StudentCourse" class="Button">Course</a>
<a href="/Student/StudentChat" class="Button">Chat</a>
</div>
</div>
</div>
<center>
<center>
<h1 style="color:white">Courses details</h1>
<table id="customers">
<tr>
<th>Course ID</th>
<th>Course Hour</th>
<th>Course Day</th>
</tr>
<tr>
@foreach (var item in Model)
{
<b>
<b>Course ID : @Model.CourseID</b>
<b>Course ID : @Model.Day</b>
<b>Course ID : @Model.Hour</b>
</b>
}
<tr>
</table>
</center>
</center>
</div>
</body>
</html>
我正在尝试做的是:
在我的数据库中有3个表,一个用于学生(其中我获取学生的ID和全名)
将其与StudentInCourse表进行比较(其中我获取学生的ID和课程的ID)
并显示在Course表中具有相同ID的所有课程的信息。
例如:
我有一个带有ID 2的学生。
我需要检查此ID是否在StudentInCourse数据库表中具有任何课程ID。(例如,学生的ID为2,他与课程ID为3的课程相关联)
如果是这样,请转到课程表,并获取所有具有ID 3的课程并显示其所有信息。
英文:
I'm trying to send a local variable which is a list and showing it at the View.
I've been trying all sorts of ways but nothing seems to be working and I'm kind of desperate.
Controller
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data.SqlClient;
using System.Data;
using Project.Models;
using System.Configuration;
using Project.Dal;
using Project.ViewModel;
namespace Project.Controllers
{
public class StudentController : Controller
{
public ActionResult StudentCourse()
{
var id = (int)Session["ID"];
StudentInCourseDal SICD = new StudentInCourseDal();
CoursesDal CD = new CoursesDal();
List<Courses> courseinfo = new List<Courses>();
//var course = (from x in SICD.StudentInCourses where x.studentID.Equals(id) select x.CourseID).ToList();
//var courses = (from x in CD.Courses where x.CourseID.Equals(course) select x.Coursesinfo).ToList();
/* get all courses ids */
var courses =
(from x in SICD.StudentInCourses
where x.studentID.Equals(id)
select x.CourseID).ToList();
/* get each course info thats in courses */
var coursesInfo =
(from x in CD.Courses
where courses.Contains(x.CourseID)
select x).ToList();
return View("StudentCourse", coursesInfo);
}
public ActionResult StudentExams()
{
return View();
}
}
}
DAL
using Project.Models;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
namespace Project.Dal
{
public class CoursesDal : DbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Courses>().ToTable("Courses");
}
public DbSet<Courses> Courses { get; set; }
}
}
Model
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace Project.Models
{
public class Courses
{
[Key]
[Required]
public int CourseID { get; set; }
[Required]
public int LectureID { get; set; }
[Required]
public string Day { get; set; }
[Required]
public TimeSpan Hour { get; set; }
[Required]
public DateTime? moedADate { get; set; }
[Required]
public DateTime? moedBDate { get; set; }
[Required]
public string moedAClass { get; set; }
[Required]
public string moedBClass { get; set; }
}
}
CourseViewModel
using Project.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace Project.ViewModel
{
public class CoursesViewModel
{
public Courses courseName { get; set; }
public List<Courses> coursesName { get; set; }
}
}
StudentCourseView
@model Project.Models.Courses
<!DOCTYPE html>
<style type="text/css">
.table1 {
width: 500px;
height: 200px;
text-align: center;
}
.cell {
border: solid 1px #999;
}
.cell-1 {
border: solid 1px #999;
background: #999;
color: #FFF;
}
</style>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>StudentCourse</title>
<link href="~/styles/font-awesome.min.css" rel="stylesheet" />
<link href="~/styles/style.css" rel="stylesheet" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link href="~/styles/navbar.css" rel="stylesheet" />
</head>
<body>
<div>
<div class="navbar">
<a href="/Account/StudentHomePage" class="Button">Home</a>
<a href="/Student/StudentSchedule" class="Button">Schedule</a>
<div class="dropdown">
<button class="dropbtn">
More
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content">
<a href="/Student/StudentExams" class="Button">Exams</a>
<a href="/Student/StudentCourse" class="Button">Course</a>
<a href="/Student/StudentChat" class="Button">Chat</a>
</div>
</div>
</div>
<center>
<center>
<h1 style="color:white">Courses details</h1>
<table id="customers">
<tr>
<th>Course ID</th>
<th>Course Hour</th>
<th>Course Day</th>
</tr>
<tr>
@foreach (var item in Model)
{
<b>
<b>Course ID : @Model.CourseID</b>
<b>Course ID : @Model.Day</b>
<b>Course ID : @Model.Hour</b>
</b>
}
<tr>
</table>
</center>
</center>
</div>
</body>
</html>
What I am trying to do is like this:
In my DB theres 3 tables, one for students (there I get ID and full name of the students)
Compare it to the table StudentInCourse (There I got ID of student and ID of course)
and show all the courses that has the same ID in the Course table.
for example:
I have student entering with ID of 2.
I need to check if this ID has any courses ID in the DB table of StudentInCourse.(example student got ID 2 and he links to courseID 3)
If it does, go to course table and take all the courses who has ID of 3 and show all their info.
答案1
得分: 1
您正在传递courseinfo
,其类型为List<Courses>
,但您的视图期望的模型类型是Project.Models.Courses
。
如果您仍然想传递Courses列表,请在视图中更改模型如下。
@model List<Project.Models.Courses>
此外,当您想要访问模型的属性时,您需要使用foreach循环来遍历列表,如下所示。
@foreach(var course in Model)
{
<b>
<b>Course ID : @course.CourseID</b>
<b>Course ID : @course.Day</b>
<b>Course ID : @course.Hour</b>
</b>
}
英文:
You are passing courseinfo
which is List<Courses>
but you your view is expecting the a model of type Project.Models.Courses
.
If you still want to pass list of Courses then change the model in the view as below.
@model List<Project.Models.Courses>
Also, when you want to access the properties of Model then you need iterate the list using foreach as below.
@foreach(var course in Model)
{
<b>
<b>Course ID : @course.CourseID</b>
<b>Course ID : @course.Day</b>
<b>Course ID : @course.Hour</b>
</b>
}
答案2
得分: 0
感谢Rejesh G,它有效了,现在显示了我所需要的所有表格!非常感激!
英文:
Thanks a lot Rejesh G, It works and now show all the table's I was needed!
Appreciate it a lot!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论