我应该通过RedirectToAction还是TempData传递值?

我已经看到一些文章(甚至MSDN)建议TempData在ActionMethods之间传递数据。 但我在这里看到其他人说TempData应该避免。 采用这种方法的最佳实践方法是什么?

这是一些显示我的情况的代码。 注意:我100%肯定,我做错了。 这就是我在这里的原因。 :)此外,我一直在做Webforms直到最近。

注2: 这是相关的,但不一样。

视图:

@using (Html.BeginForm("Previous", "Home", new {month = @month}, FormMethod.Post)) { } // This fails but that's another situation @using (Html.BeginForm("Next", "Home", new {month = @month, year = @year}, FormMethod.Post)) { }

控制器方法:

 [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 }); } 

听起来你将你的动作方法与最终结果紧密结合起来。

我会稍微重构一下; 你会有这样的索引方法:

  public ActionResult Index() { HTMLMVCCalendar.Models.MonthModel someModel = new HTMLMVCCalendar.Models.MonthModel(); someModel.DateTime = DateTime.Now; // whatever return View(someModel); } 

然后,当您需要重新计算日历时,只需发布​​到相同的URL,该URL返回具有新视图模型数据的相同视图。

  [HttpPost] public ActionResult Index(HTMLMVCCalendar.Models.MonthModel previousModel, bool? goForward) { if(goForward.HasValue && goForward.Value) previousModel.DateTime = previousModel.DateTime.AddMonths(1); else previousModel.DateTime = previousModel.DateTime.AddMonths(-1); return View(previousModel); } 

您保持相同的URL并显示相同的视图,但具有您需要的更改。 每个操作都不需要特定的端点。

我想出了这个。 这是坏的,好的,应该改进吗?

RAZOR / HTML:

 
@using (Html.BeginForm("Previous", "Home", new{ year = @year, month = @month }, FormMethod.Post)) { }         @using (Html.BeginForm("Next", "Home", new { year = @year, month = @month }, FormMethod.Post)) { }

控制器/动作方法:

 public ActionResult Index(int? year = 2012 , int? month = 2) { ViewBag.Message = "Welcome to ASP.NET MVC!"; Calendar monthEventsCal = new Calendar(); HTMLMVCCalendar.Models.MonthModel allMonthEvents = monthEventsCal.monthEvents(year.Value, month.Value); return View("Index", allMonthEvents); } [HttpPost] public ActionResult Previous(int? year = 2012, int? month = 2) { Calendar monthEventsCal = new Calendar(); var newMonth = monthEventsCal.previousMonth(year.Value, month.Value); int currMonth = newMonth.Item2; int currYear = newMonth.Item1; return RedirectToAction("Index", "Home", new { month = currMonth, year = currYear }); } [HttpPost] public ActionResult Next(int? year = 2012, int? month = 2) { Calendar monthEventsCal = new Calendar(); var newMonth = monthEventsCal.nextMonth(year.Value, month.Value); int currMonth = newMonth.Item2; int currYear = newMonth.Item1; return RedirectToAction("Index", "Home", new { month = currMonth, year = currYear }); } 

的Global.asax.cs:

 public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( "Default", // Route name "{controller}/{action}/{month}/{year}", // URL with parameters new { controller = "Home", action = "Index", month = UrlParameter.Optional, year = UrlParameter.Optional } // Parameter defaults //"{controller}/{action}/{id}", // URL with parameters //new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults ); }