传递(局部变量)List<Courses> 并在视图 MVC 中显示列表

Passing (local variable)List<Courses> and showing the list at the view MVC

我正在尝试发送一个局部变量,它是一个列表并在视图中显示它。 我一直在尝试各种方法,但似乎没有任何效果,我有点绝望。

控制器


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; }
    }
} 

型号

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 个 tables,一个给学生(在那里我得到了学生的 ID 和全名) 将其与 table StudentInCourse 进行比较(我得到了学生 ID 和 ID 当然) 并显示 Course table.

中具有相同 ID 的所有课程

例如:

我有 ID 为 2 的学生进入。 我需要检查此 ID 在 StudentInCourse 的数据库 table 中是否有任何课程 ID。(示例学生获得 ID 2,他链接到 courseID 3) 如果是,请转到课程 table 并选择 ID 为 3 的所有课程并显示他们的所有信息。

您传递的是 courseinfo,即 List<Courses>,但您的视图需要 Project.Models.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> 
}

非常感谢 Rejesh G,它工作正常,现在显示我需要的所有 table! 非常感谢! :D