所以我的日曆應用程序終於正常工作。但是這個視圖似乎做得太多了。它超越了模板。如果你同意,我可以做什麼來做得更好?我的Razor中的代碼太多了 - 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>
我已經看到了更大的看法。這裏沒有什麼是對我投擲警報;然而,如果你願意,你總是可以爲代表日曆的自定義類型創建一個DisplayTemplate。 – Tejs 2012-04-17 19:29:25