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

时间:2012-04-17 15:00:09

标签: c# asp.net-mvc asp.net-mvc-3 redirecttoaction tempdata

我见过一些文章(甚至是MSDN)建议TempData在ActionMethods之间传递数据。但我在这里看到其他人说TempData应该避免。处理此问题的最佳做法是什么?

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

注2:This is related, but not the same.

查看:

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

2 个答案:

答案 0 :(得分:2)

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

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

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

您使用相同的网址并显示相同的视图,但需要进行更改。每个操作都不需要特定的端点。

答案 1 :(得分:0)

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

剃刀/ HTML:

<div>
        @using (Html.BeginForm("Previous", "Home", new{ year = @year, month = @month },  FormMethod.Post)) 
        {
            <input id="previous" type="submit" value="Previous" />
        }
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        @using (Html.BeginForm("Next", "Home", new { year = @year, month = @month }, FormMethod.Post))
        {
            <input id="next" type="submit" value="Next" />
        }
    </div>

控制器/行动方法:

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

        }