I am transforming my way of thinking and programming from Webforms to MVC3. For the record, my head hurts.
So, I create a calendar with a month (only days and events for this month are displayed on the page). I put two buttons Previous and Next to go from month to month. In Webforms, any change will only happen in Calendar.aspx. However, it seems that I will have to do two ActionResults (both HttpPost). One goes to the previous view, and the other to the next view. Just to get the same effect. I was hoping to just update the calendar in Index ActionResult / View, but I don't see how to do it. Below is my index.
In short, is there a way to update the calendar and return to the Index view? Or do I need to do two other views (Previous and Next) to show the new month when I click the corresponding buttons (Previous and Next)?
@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;
}
<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:
</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) + 1); ++j)
{
<td>
<div><span>@startCount</span></div>
<div>
<span>
@{
var todaysEvents = Model.AllDays.ToList().FindAll(d => d.CalDate.Day == startCount);
foreach(HTMLMVCCalendar.Models.CalendarModel 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>
<input id="previous" type="submit" value="submit" />
<input id="next" type="submit" value="submit" />
</div>
</div>
UPDATE:
I have tried this and I think it should work. Not sure why this didn't happen?
<div>
@using (Html.BeginForm("Previous", "Home"))
{
<input id="previous" type="submit" value="Previous" />
}
@using (Html.BeginForm("Next", "Home"))
{
<input id="next" type="submit" value="Next" />
}
</div>
public ActionResult Index()
{
ViewBag.Message = "Welcome to ASP.NET MVC!";
int month;
int year;
int.TryParse(ViewBag.Year, out year);
int.TryParse(ViewBag.Month, out month);
Calendar monthEventsCal = new Calendar();
HTMLMVCCalendar.Models.MonthModel allMonthEvents = monthEventsCal.monthEvents(year, month);
return View("Index", allMonthEvents);
}
[HttpPost]
public ActionResult Previous(HTMLMVCCalendar.Models.MonthModel prevMonth)
{
Calendar monthEventsCal = new Calendar();
var newMonth = monthEventsCal.previousMonth(prevMonth.Year, prevMonth.Month);
ViewBag.Month = newMonth.Item2;
ViewBag.Year = newMonth.Item1;
return RedirectToAction("Index");
}
[HttpPost]
public ActionResult Next(HTMLMVCCalendar.Models.MonthModel nextMonth)
{
Calendar monthEventsCal = new Calendar();
var newMonth = monthEventsCal.nextMonth(nextMonth.Year, nextMonth.Month);
ViewBag.Month = newMonth.Item2;
ViewBag.Year = newMonth.Item1;
return RedirectToAction("Index");
}
source
share