所以我的日历应用程序终于可以正常工作了。但似乎 View 做得太多了。它已经超越了模板。如果您同意,我可以/应该做些什么来使它变得更好?
相关:控制器代码隐藏
@model HTMLMVCCalendar.Models.MonthModel
@{
ViewBag.Title = "Home Page";
int month = Model.Month;
int year = Model.Year;
int numberOfDays = DateTime.DaysInMonth(year, month);
int startDay = (int)(Convert.ToDateTime(month + "/1/" + year).DayOfWeek);
int startCount = 1;
}
@*Need to have querystring to hold month and year.*@
<h2>@ViewBag.Message</h2>
<p>
To learn more about ASP.NET MVC visit <a href="http://asp.net/mvc" title="ASP.NET MVC Website">http://asp.net/mvc</a>.
</p>
<div>
<table id="calendar">
<thead>
<tr>
<th id="monthHeader" colspan="7">
<h3>
@{
@Convert.ToDateTime(month + "/1/" + year).ToString("MMMM");
}
</h3>
</th>
</tr>
<tr>
<th>Sun</th>
<th>Mon</th>
<th>Tues</th>
<th>Wed</th>
<th>Thur</th>
<th>Fri</th>
<th>Sat</th>
</tr>
</thead>
<tbody>
<tr>
@for (int i = 0; i < startDay; ++i)
{
@:<td><div><span> </span></div><div><span> </span></div></td>
}
@for (int j = startDay; j < ((numberOfDays + startDay)); ++j)
{
<td>
<div><span>@startCount</span></div>
<div>
<span>
@{
var todaysEvents = Model.AllDays.ToList().FindAll(d => d.CalDate.Day == startCount);
foreach(HTMLMVCCalendar.Models.DayModel eventsToday in todaysEvents)
{
foreach(HTMLMVCCalendar.Models.EventModel eventToday in eventsToday.CalEvents)
{
<text>
@eventToday.DayCode:<br />
@eventToday.Subject:<br />
@eventToday.EventDesc<br /><br />
</text>
}
}
}
</span>
</div>
</td>
if ((j + 1) % 7 == 0)
{
@:</tr><tr>
}
++startCount;
}
</tr>
</tbody>
</table>
<div>
<table>
<tr>
<td>
@using (Html.BeginForm("Previous", "Home", new{ year = @year, month = @month }, FormMethod.Post))
{
<input id="previous" type="submit" value="Previous" />
}
</td>
<td>
@using (Html.BeginForm("Next", "Home", new { year = @year, month = @month }, FormMethod.Post))
{
<input id="next" type="submit" value="Next" />
}
</td>
</tr>
</table>
</div>
</div>