I need users to be able to input hours into a table, potentially with fractional numbers, as follows:
I've declared the following classes to hold the model data:
public class WeeklyTimes
{
public decimal Week1 { get; set; }
public decimal Week2 { get; set; }
public decimal Week3 { get; set; }
public decimal Week4 { get; set; }
public decimal Week5 { get; set; }
}
public class MonthlyReport
{
public WeeklyTimes TimeSpentTutoring { get; set; }
public WeeklyTimes TimeSpentOnPreparation { get; set; }
public WeeklyTimes TimeSpentOnHomework { get; set; }
}
And binding to the following view:
<table class="timesheet">
<tr class="timesheet-row">
<th class="center-align" />
<th class="center-align">Week 1</th>
<th class="center-align">Week 2</th>
<th class="center-align">Week 3</th>
<th class="center-align">Week 4</th>
<th class="center-align">Week 5</th>
</tr>
<tr class="timesheet-row">
<th class="left-align">Tutoring Time</th>
<td>@Html.TextBoxFor(x => x.TimeSpentTutoring.Week1, new { @class = "input-time" })</td>
<td>@Html.TextBoxFor(x => x.TimeSpentTutoring.Week2, new { @class = "input-time" })</td>
<td>@Html.TextBoxFor(x => x.TimeSpentTutoring.Week3, new { @class = "input-time" })</td>
<td>@Html.TextBoxFor(x => x.TimeSpentTutoring.Week4, new { @class = "input-time" })</td>
<td>@Html.TextBoxFor(x => x.TimeSpentTutoring.Week5, new { @class = "input-time" })</td>
</tr>
<tr class="timesheet-row">
<th class="left-align">Learner Homework</th>
<td>@Html.TextBoxFor(x => x.TimeSpentOnHomework.Week1, new { @class = "input-time" })</td>
<td>@Html.TextBoxFor(x => x.TimeSpentOnHomework.Week2, new { @class = "input-time" })</td>
<td>@Html.TextBoxFor(x => x.TimeSpentOnHomework.Week3, new { @class = "input-time" })</td>
<td>@Html.TextBoxFor(x => x.TimeSpentOnHomework.Week4, new { @class = "input-time" })</td>
<td>@Html.TextBoxFor(x => x.TimeSpentOnHomework.Week5, new { @class = "input-time" })</td>
</tr>
<tr class="timesheet-row">
<th class="left-align">Tutor Prep & Commute</th>
<td>@Html.TextBoxFor(x => x.TimeSpentOnPreparation.Week1, new { @class = "input-time" })</td>
<td>@Html.TextBoxFor(x => x.TimeSpentOnPreparation.Week2, new { @class = "input-time" })</td>
<td>@Html.TextBoxFor(x => x.TimeSpentOnPreparation.Week3, new { @class = "input-time" })</td>
<td>@Html.TextBoxFor(x => x.TimeSpentOnPreparation.Week4, new { @class = "input-time" })</td>
<td>@Html.TextBoxFor(x => x.TimeSpentOnPreparation.Week5, new { @class = "input-time" })</td>
</tr>
</table>
Two parts to this question:
- I'm new to MVC, and feel unsure about the amount of duplication in the HTML. Is there a better way to structure this HTML?
- What would be the best way to ensure appropriate validation? Ideally the user would only be able to enter numbers, formatted as follows: "#.#". However, using the decimal data-type, I can't seem to make this happen. I'm also consider using a string data-type and regular expression validation, but that seems strange to me.