我有一個顯示我多麼希望它顯示的考勤表,但一個不能編輯作爲尚未放在桌子上的任何信息。如何爲ViewModel生成的真/假值表編輯器?
就設計而言,我希望管理員能夠登錄,查看錶格以及(如果一份考勤數據需要從「當前」改變爲「缺席」(或者,實際上是「真實的「to false」在實際數據庫中)單擊「present」或「absent」將數據切換到相反的值。
這是一個很好的設計理念嗎?更明智的決定去下拉,所以每一塊數據可以編輯,然後有一個「提交更改」按鈕,並提交所有更改一次(而不是每次編輯後查詢整個表)?
什麼是最好的用t創建頁面的方法他的ViewModel可編輯?
我以爲我會編輯視圖模型的模型和我通過編輯視圖模型後回到控制器中郵行動我會所有的新數據綁定到相應的數據庫數據。然而,由於我設置這個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>
這產生了表看起來像這樣: