0

我有一个考勤表,显示我希望它如何显示,但目前还不能编辑表上的任何信息。

就设计而言,我希望管理员能够登录,查看表格,并且(如果一条考勤数据需要从“存在”更改为“缺席”(或者,实际上,“真”到“假”)在实际数据库中)单击单词“present”或“absent”将数据切换到相反的值。

对于这种类型的桌子,这是一个好的设计理念吗?使用下拉菜单会是一个更明智的决定,这样每条数据都可以被编辑,然后有一个“提交更改”按钮,所有更改都提交一次(而不是在每次编辑后查询整个表)?

使使用此 ViewModel 创建的页面可编辑的最佳方法是什么?

我假设我会编辑 ViewModel 的模型,并且在将编辑后的 ​​ViewModel 传递回 Post 操作中的控制器后,我会将所有新数据绑定到适当的数据库数据。但是,由于我在设置这个 ViewModel 时遇到了困难(我从 stackoverflow 上的另一篇文章中得到了很多帮助),我不确定如何重新绑定所有更新的信息。

任何正确方向的提示都会有所帮助。

以下是所有相关信息:

实际的课堂课程或课程:

public class Course
{
    public int CourseID { get; set; }
    public string Title { get; set; }
    public int AttendanceDate { get; set;}
    public virtual ICollection<Enrollment> Enrollments { get; set; } // allows Students to be enrolled in a Course
    etc. . .
}

我的学生们:

public class Student
{
    public int StudentID { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Enrollment> Enrollments { get; set; } // allows Student to be enrolled in a Course
    etc. . .
}

将学生与课程联系起来的实体:

public class Enrollment
{
    public int EnrollmentID { get; set; }
    public int CourseID { get; set; }
    public int StudentID { get; set; }
    public virtual Student Course { get; set; }
    public virtual Student Student { get; set; }
}

出勤数据:

public class Attendance
{
    public int AttendanceID { get; set; }
    public int CourseID { get; set; }
    public int StudentID { get; set; }
    public int AttendanceDay { get; set; }  // used to set how many days people are supposed to attend this Course (each course has a different length, some are 10 day courses, some are 3, etc.)
    public bool Present { get; set; }  // absent or present (set to absent by default)
    public virtual Course Course { get; set; }
    public virtual Student Student { get; set; }
}

我的视图模型:

public class AttendanceReportViewModel
{
    public List<int> AttendanceDays { get; set; }
    public List<Student> Students { get; set; }
    public List<Attendance> Attendances { get; set; }

    public string IsPresent(Student student, int attendanceDay)
    {
        return Attendances.Single(a => a.StudentID == student.StudentID && a.AttendanceDay == attendanceDay).Present ? "present" : "absent";
    }
}

我的观点:

<table>
    <thead>
        <tr>
            <th>Attendance Day</th>
            @foreach (var attendanceDay in Model.AttendanceDays)
            {
                <th>@attendanceDay</th>
            }
        </tr>
    </thead>
    <tbody>
        @foreach (var student in Model.Students)
        {
            <tr>
                <td>@student.LastName</td>
                @foreach (var attendanceDay in Model.AttendanceDays)
                {
                    <td>@Model.IsPresent(student, attendanceDay)</td>
                }
            </tr>
        }
    </tbody>
</table>

这会产生一个如下所示的表:

在此处输入图像描述

4

1 回答 1

1

使使用此 ViewModel 创建的页面可编辑的最佳方法是什么?->

我更喜欢包含 2 个项目的下拉列表(组合框)来选择“存在”和“缺席”。

其次是将选项“缺席”的颜色更改为红色,以便突出显示。

您需要使用 @Html.DropDownListFor() 做一些工作以使其正确或使用原始 html 和标签。下拉示例

最后,要将考勤表发回服务器,我们需要在视图端创建一个表单,将考勤表发回服务器,然后服务器将更新考勤并返回视图。

如果用户是非管理员用户,则呈现的视图不应是可编辑的。

可回传更改考勤或记录新考勤的模型:

public class AttendanceItem
{
    public int AttendanceId {get; set;}
    public int Day {get; set;}
    public int StudentId {get; set;}
    public bool Present { get; set; }
}

行动方法:

[HttpPost]
public ActionResult AttendanceView(List<AttendanceItem> AttendanceSheet, int courseId)
{
     //Update the database with AttendanceSheet for course ID.....
     return RedirectToAction("AttendanceView", new { id = courseId });  
}
于 2012-05-24T07:35:34.213 回答