0

所以我的日历应用程序终于可以正常工作了。但似乎 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>&nbsp;</span></div><div><span>&nbsp;</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>
                                            &nbsp;@eventToday.DayCode:<br />
                                            &nbsp;@eventToday.Subject:<br />
                                            &nbsp;@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>
4

2 回答 2

3

看起来不错,我唯一的建议是将以下逻辑移到您的模型中

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;
于 2012-04-17T19:42:55.507 回答
2

这看起来不错的样子。它显示了一个组织良好的结构,只有足够的脚本来提供该特定页面上的要点。我会说这并不过分。当您组织代码时,它可能看起来就是这样。组织是大型项目的关键,所以你要养成一个好习惯。

于 2012-04-17T19:29:21.337 回答