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

huangapple go评论60阅读模式
英文:

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[&quot;ID&quot;];

            StudentInCourseDal SICD = new StudentInCourseDal();
            CoursesDal CD = new CoursesDal();
            List&lt;Courses&gt; courseinfo = new List&lt;Courses&gt;();
            //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(&quot;StudentCourse&quot;, 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&lt;Courses&gt;().ToTable(&quot;Courses&quot;);
        }
        public DbSet&lt;Courses&gt; 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&lt;Courses&gt; coursesName { get; set; }
    }
}

StudentCourseView

@model Project.Models.Courses
&lt;!DOCTYPE html&gt;
&lt;style type=&quot;text/css&quot;&gt;
    .table1 {
        width: 500px;
        height: 200px;
        text-align: center;
    }

    .cell {
        border: solid 1px #999;
    }

    .cell-1 {
        border: solid 1px #999;
        background: #999;
        color: #FFF;
    }
&lt;/style&gt;
&lt;html&gt;

&lt;head&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width&quot; /&gt;
    &lt;title&gt;StudentCourse&lt;/title&gt;
    &lt;link href=&quot;~/styles/font-awesome.min.css&quot; rel=&quot;stylesheet&quot; /&gt;
    &lt;link href=&quot;~/styles/style.css&quot; rel=&quot;stylesheet&quot; /&gt;
    &lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css&quot;&gt;
    &lt;link href=&quot;~/styles/navbar.css&quot; rel=&quot;stylesheet&quot; /&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;div&gt;
        &lt;div class=&quot;navbar&quot;&gt;
            &lt;a href=&quot;/Account/StudentHomePage&quot; class=&quot;Button&quot;&gt;Home&lt;/a&gt;
            &lt;a href=&quot;/Student/StudentSchedule&quot; class=&quot;Button&quot;&gt;Schedule&lt;/a&gt;
            &lt;div class=&quot;dropdown&quot;&gt;
                &lt;button class=&quot;dropbtn&quot;&gt;
                    More
                    &lt;i class=&quot;fa fa-caret-down&quot;&gt;&lt;/i&gt;
                &lt;/button&gt;
                &lt;div class=&quot;dropdown-content&quot;&gt;
                    &lt;a href=&quot;/Student/StudentExams&quot; class=&quot;Button&quot;&gt;Exams&lt;/a&gt;
                    &lt;a href=&quot;/Student/StudentCourse&quot; class=&quot;Button&quot;&gt;Course&lt;/a&gt;
                    &lt;a href=&quot;/Student/StudentChat&quot; class=&quot;Button&quot;&gt;Chat&lt;/a&gt;

                &lt;/div&gt;

            &lt;/div&gt;
        &lt;/div&gt;

        &lt;center&gt;
            &lt;center&gt;
                &lt;h1 style=&quot;color:white&quot;&gt;Courses details&lt;/h1&gt;
                &lt;table id=&quot;customers&quot;&gt;
                    &lt;tr&gt;
                        &lt;th&gt;Course ID&lt;/th&gt;
                        &lt;th&gt;Course Hour&lt;/th&gt;
                        &lt;th&gt;Course Day&lt;/th&gt;

                    &lt;/tr&gt;
                    &lt;tr&gt;
                        @foreach (var item in Model)
                        {
                         &lt;b&gt;
                            &lt;b&gt;Course ID : @Model.CourseID&lt;/b&gt;
                            &lt;b&gt;Course ID : @Model.Day&lt;/b&gt;
                            &lt;b&gt;Course ID : @Model.Hour&lt;/b&gt;
                         &lt;/b&gt;  
                        }

                        &lt;tr&gt;

                    &lt;/table&gt;
                &lt;/center&gt;
            &lt;/center&gt;

    &lt;/div&gt;

&lt;/body&gt;
&lt;/html&gt;

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&lt;Courses&gt;,但您的视图期望的模型类型是Project.Models.Courses

如果您仍然想传递Courses列表,请在视图中更改模型如下。

@model List&lt;Project.Models.Courses&gt;

此外,当您想要访问模型的属性时,您需要使用foreach循环来遍历列表,如下所示。

@foreach(var course in Model)
{
    &lt;b&gt;
    &lt;b&gt;Course ID : @course.CourseID&lt;/b&gt;
    &lt;b&gt;Course ID : @course.Day&lt;/b&gt;
    &lt;b&gt;Course ID : @course.Hour&lt;/b&gt;
    &lt;/b&gt;
}
英文:

You are passing courseinfo which is List&lt;Courses&gt; 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&lt;Project.Models.Courses&gt;

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)
{
 &lt;b&gt;
                        &lt;b&gt;Course ID : @course.CourseID&lt;/b&gt;
                        &lt;b&gt;Course ID : @course.Day&lt;/b&gt;
                        &lt;b&gt;Course ID : @course.Hour&lt;/b&gt;
                     &lt;/b&gt; 
}

答案2

得分: 0

感谢Rejesh G,它有效了,现在显示了我所需要的所有表格!非常感激! 传递(局部变量)List<Courses> 并在视图 MVC 中显示列表。

英文:

Thanks a lot Rejesh G, It works and now show all the table's I was needed!
Appreciate it a lot! 传递(局部变量)List<Courses> 并在视图 MVC 中显示列表。

huangapple
  • 本文由 发表于 2020年1月3日 19:26:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/59577790.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定