0

So my Calendar app is finally working properly. But it seems that the View is doing too much. It's gone beyond templating. If you agree, what can/should I do to make it better?

Related: 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>
Community
  • 1
  • 1
dotnetN00b
  • 5,021
  • 13
  • 62
  • 95
  • 1
    I've seen bigger views. Nothing here is throwing alarms at me; however, if you wanted, you could always create a DisplayTemplate for a custom type that represents you calendar. – Tejs Apr 17 '12 at 19:29

2 Answers2

3

looks good, the only suggestion i have is to move the following logic into your model

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;
c0deNinja
  • 3,956
  • 1
  • 29
  • 45
  • Well the MonthModel only has the month, year, and only the days that have events. Is it a good idea to have two models in a View? – dotnetN00b Apr 18 '12 at 00:03
  • 2
    Generally I create ViewModel which contains all the models which I use in my View. You can also create a ViewModel class which will contain your actual model and these properties (year,month,etc) and bind your view with your ViewModel class. – Zeeshan Umar Apr 18 '12 at 07:50
2

It looks good. It shows a very well-organized structure with just enough script to provide the essentials on that particular page. I would say that it's not over-doing it. It may seem that way when you organize your code. Organization is key to large projects, so you're practicing a good habit.

Anthony
  • 496
  • 3
  • 9