这是我的剃刀中的太多代码 - 查看?

时间:2012-04-17 19:25:53

标签: html asp.net-mvc razor

所以我的日历应用程序终于正常运行了。但看起来View看起来太过分了。它超越了模板。如果您同意,我可以/应该做些什么来改善它?

相关:Controller code-behind

@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>

2 个答案:

答案 0 :(得分: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;

答案 1 :(得分:2)

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