我正在将我的思维和编程方式从 Webforms 转换为 MVC3。记录在案,这让我很头疼。
所以我正在制作一个具有月视图的日历(页面仅显示该月的日期和事件)。我将两个按钮“上一个”和“下一个”按月放置。在 Webforms 中,任何更改都只会发生在 Calendar.aspx 上。但是,似乎我必须制作两个 ActionResults(都是 HttpPost)。一个进入上一个视图,另一个进入下一个视图。只是为了达到同样的效果。我希望只更新 Index ActionResult/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;
}
<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) + 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>
更新: 我已经尝试过了,我认为它应该可以工作。不知道为什么没有?
<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");
}