我需要用户能够在表格中输入小时数,可能带有小数,如下所示:
我已经声明了以下类来保存模型数据:
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; }
}
并绑定到以下视图:
<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>
这个问题的两个部分:
- 我是 MVC 的新手,不确定 HTML 中的重复数量。有没有更好的方法来构建这个 HTML?
- 确保适当验证的最佳方法是什么?理想情况下,用户只能输入数字,格式如下:“#.#”。但是,使用十进制数据类型,我似乎无法做到这一点。我也考虑使用字符串数据类型和正则表达式验证,但这对我来说似乎很奇怪。