如何使用arraylist作为模型在视图中显示数据

时间:2017-02-11 19:11:05

标签: asp.net-mvc

我有一个像这样的动作: 我不得不通过arraylist中的不同列表发送数据 但无论我做什么,我都无法在视图中正确显示数据

public ActionResult StudentMainPage(string name)
    {
        ViewBag.studentName = name;
        string[] fullName = name.Split(' ');
        string firstName = fullName[0];
        string lastname = fullName[1];
        List<string> CoursesName = new List<string>();
        List<string> teachersName = new List<string>();
        List<string> daysList = new List<string>();
        List<int?> classnumberList = new List<int?>();

        Guid studentId =
            db.Students.Where(s => s.StudentName == firstName && s.StudentLastName == lastname)
                .Select(s => s.Student_Id)
                .First();
        var relatedInfo = db.USP_GetClassInfoFroStudent(studentId).ToList();
        foreach (var item in relatedInfo)
        {
            CoursesName.Add(db.Courses.Where(c => c.CourseId == item.CourseId).Select(c => c.CourseName).First());
            teachersName.Add(db.Teachers.Where(t => t.Teacher_id == item.Teacher_Id)
                .Select(t => t.TeacherLastName)
                .First());
            daysList.Add(
                db.ClassMembers.Where(s => s.DaysInweek == item.DaysInweek).Select(d => d.DaysInweek).First());
            classnumberList.Add(
                db.ClassMembers.Where(n => n.classNumber == item.classNumber).Select(n => n.classNumber).First());
        }
        ArrayList data = new ArrayList();
        data.Add(CoursesName);
        data.Add(teachersName);
        data.Add(daysList);
        data.Add(classnumberList);

        return View(data);

这是我的观点:

@using System.Collections
@model ArrayList
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>StudentMainPage</title>
    <script src="~/Scripts/jquery-3.1.1.min.js"></script>
</head>
<body>
<div>

    <table>
        <tr>
            <Td id="name_">@ViewBag.studentName</Td>
            <td>Welcome</td>
        </tr>
        <tr>
            @foreach (var items in Model as ArrayList)
            {
                List<string> courseName = Model[0] as List<string>;
                List<string> teachersName = Model[1] as List<string>;
                List<string> daysList = Model[2] as List<string>;
                List<int?> classnumeber = Model[3] as List<int?>;
                foreach (var course in courseName)
                {
                    <td>@courseName</td>
                }
                foreach (var tName in teachersName)
                {
                    <td>@tName</td>
                }
                foreach (var lstdays in daysList)
                {
                    <td>@lstdays</td>
                }
                foreach (var classNo in classnumeber)
                {
                    <td>@classNo</td>
                }
            }

        </tr>
    </table>

</div>
</body>
</html>

结果是这样的,仅适用于数据库中的1条记录:

System.Collections.Generic.List`1[System.String]    Akasheh  1 sunday
System.Collections.Generic.List`1[System.String]    Akasheh 1 sunday
System.Collections.Generic.List`1[System.String]    Akasheh sunday

1 个答案:

答案 0 :(得分:0)

我认为你应该纠正你的外部for loop

@foreach (var items in Model as ArrayList)

当您循环遍历ArrayList模型中的每个元素时,没有必要再次使用索引访问它的项目,因为它将重复所有4次。

List<string> courseName = Model[0] as List<string>;
List<string> teachersName = Model[1] as List<string>;
List<string> daysList = Model[2] as List<string>;
List<int?> classnumeber = Model[3] as List<int?>;

我建议删除外部for loop