我有一个考勤表,显示我希望它如何显示,但目前还不能编辑表上的任何信息。
就设计而言,我希望管理员能够登录,查看表格,并且(如果一条考勤数据需要从“存在”更改为“缺席”(或者,实际上,“真”到“假”)在实际数据库中)单击单词“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>
这会产生一个如下所示的表: