我看过一些文章(甚至 MSDN)建议使用 TempData 在 ActionMethods 之间传递数据。但是我在这里看到其他人说应该避免使用 TempData。解决此问题的最佳实践方法是什么?
这是一些代码来显示我的情况。注意:我 100% 确定,我做错了。这就是我在这里的原因。:) 另外,直到最近我一直在做 Webforms。
注2:这是相关的,但不一样。
看法:
<div>
@using (Html.BeginForm("Previous", "Home", new {month = @month}, FormMethod.Post))
{
<input id="previous" type="submit" value="Previous" />
}
// This fails but that's another situation
@using (Html.BeginForm("Next", "Home", new {month = @month, year = @year}, FormMethod.Post))
{
<input id="next" type="submit" value="Next" />
}
</div>
控制器方法:
[HttpPost]
public ActionResult Previous(HTMLMVCCalendar.Models.MonthModel prevMonth)
{
Calendar monthEventsCal = new Calendar();
int month = prevMonth.Month;
int year = prevMonth.Year;
var newMonth = monthEventsCal.previousMonth(year, month);
month = newMonth.Item2;
year = newMonth.Item1;
return RedirectToAction("Index", "Home", new { month = month });
}
[HttpPost]
public ActionResult Next(HTMLMVCCalendar.Models.MonthModel nextMonth)
{
Calendar monthEventsCal = new Calendar();
int month = nextMonth.Month;
int year = nextMonth.Year;
var newMonth = monthEventsCal.nextMonth(year, month);
month = newMonth.Item2;
year = newMonth.Item1;
return RedirectToAction("Index", "Home", new { year = year, month = month });
}