I have a timetable for student so they can attend to course on the week.
And I don’t know how I should build my ViewModel.
I decided this :
class CourseTableViewModel
{
CourseTableHeaderViewModel[] Headers;
CourseTableRowViewModel[] Rows;
}
class CourseTableRowViewModel
{
int HourStart;
CourseTableCellViewModel[] Cells;
}
class CourseTableHeaderViewModel
{
DateTime Date;
}
class CourseTableCellViewModel
{
CourseViewModel[] Courses;//null if no course at this time
}
but for me it seems like to heavy for my view model. Maybe I should only send the CourseViewModel[] and then on my cshtml do all the table/row/cell work.
Does the viewmodel should really look like my view ?
EDIT : I’ll show my data as a Time Table with the days of the week as headers, and a row foreach hour of the day.
If a course take 2 hour it’ll occupy 2 rows.
There’ll be a button “Attend” or “Cancel” on each course’s cell.
PS : I know about jquery fullcalendar, but I’m just trying to learn how to build my viewmodel here.
The way you build your ViewModel MUST NOT depend on the way the View is organized, otherwise you break separation of concerns between View and Controller.
The fact that you decided to use a kind of table to show your courses MUS NOT be reflected on the ViewModel, otherwise a change in the way the View is organized would cause a chain reaction on the controller code.
Pass to the View just ALL INFORMATION it needs to render the courses, and then, in the View do all JOB that is needed to show your table.
Doing transformations of data in the View may cause problems when you are in edit mode because the inverse transformation is not applied by the model binder when it receives the posted data. If you have this problem give a look to Mvc Controls Toolkit in-line transformations there: http://mvccontrolstoolkit.codeplex.com/wikipage?title=In-Line%20Transformations