【问题标题】:Should I be passing values through RedirectToAction or TempData?我应该通过 RedirectToAction 还是 TempData 传递值?
【发布时间】:2012-04-28 22:32:04
【问题描述】:

我看过一些文章(甚至是 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 });
}

【问题讨论】:

  • 定义您需要共享的数据类型(以及如何共享),我们可以建议比 TempData 更好的替代方案,因为它作为一种良好的数据传输机制并不可靠。
  • 索引加载日历及其事件。 Previous 和 Next 按钮需要将月份和年份(例如:4 和 2012)发送到 Previous 和 Next 操作方法。他们反过来计算所需的月份和年份,并将其传递给 Index actionmethod。
  • 请看这个,它可能对你有帮助,stackoverflow.com/questions/7993263/…

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


【解决方案1】:

听起来您将操作方法​​与最终结果耦合得太紧了。

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

 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 上并呈现相同的视图,但您需要进行更改。您不需要为每个操作指定一个特定的端点。

【讨论】:

    【解决方案2】:

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

    剃刀/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
                );
    
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-20
      • 2023-04-07
      • 1970-01-01
      • 1970-01-01
      • 2015-04-10
      • 2012-11-13
      • 2020-04-01
      • 1970-01-01
      相关资源
      最近更新 更多