我应该通过RedirectToAction或TempData的路过值? [英] Should I be passing values through RedirectToAction or TempData?

查看:123
本文介绍了我应该通过RedirectToAction或TempData的路过值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到一些文章(甚至MSDN)建议的TempData为ActionMethods之间传递数据。但我看到别人在这里说的TempData应避免。什么是最好的实践方式来处理这个?

下面是一些code,以显示我的处境。
注:我100%肯定,我这样做是错误的。这就是为什么我在这里。 :)另外,我一直在做Web表单,直到最近。

注2:<一href=\"http://stackoverflow.com/questions/10126989/can-data-be-updated-through-the-index-view-using-buttons\">This是相关的,但并不相同。

查看:

 &LT; D​​IV&GT;
    @using(Html.BeginForm(previous,家,新的{月= @month},FormMethod.Post))
    {
        &LT;输入ID =previous类型=提交值=previous/&GT;
    }    //这个失败,但那是另一种情况
    @using(Html.BeginForm(下一步,家,新的{月= @month,一年= @year},FormMethod.Post))
    {
        &LT;输入ID =下一步类型=提交值=下一步/&GT;
    }
&LT; / DIV&GT;

控制器方法:

  [HttpPost]
公众的ActionResult previous(HTMLMVCCalendar.Models.MonthModel prevMonth)
{
    日历monthEventsCal =新的日历();    INT月= prevMonth.Month;
    年整型= prevMonth.Year;    。VAR newMonth = monthEventsCal previousMonth(年,月);    月= newMonth.Item2;
    年= newMonth.Item1;    返回RedirectToAction(指数,家,新的{月=每月});
}[HttpPost]
公众的ActionResult下一页(HTMLMVCCalendar.Models.MonthModel nextMonth)
{
    日历monthEventsCal =新的日历();    INT月= nextMonth.Month;
    年整型= nextMonth.Year;    VAR newMonth = monthEventsCal.nextMonth(年,月);    月= newMonth.Item2;
    年= newMonth.Item1;    返回RedirectToAction(指数,家,新的一年{=年,月=月});
}


解决方案

这听起来像你太紧密耦合动作方法到你的最终结果。

我要重构一点点;你有你的指数法,像这样:

 公众的ActionResult指数()
 {
      HTMLMVCCalendar.Models.MonthModel someModel =新HTMLMVCCalendar.Models.MonthModel();      someModel.DateTime = DateTime.Now; // 随你      返回查看(someModel);
 }

然后,当你需要重新计算你的日历,你只需发布到返回新的视图模型的数据相同的观点相同的URL。

  [HttpPost]
 公众的ActionResult指数(HTMLMVCCalendar.Models.MonthModel previousModel,布尔?goForward)
 {
      如果(goForward.HasValue&安培;&安培; goForward.Value)
            previousModel.DateTime = previousModel.DateTime.AddMonths(1);
      其他
            previousModel.DateTime = previousModel.DateTime.AddMonths(-1);      返回查看(previousModel);
 }

您留在同一个URL和present了同样的观点,但你需要的变化。你不需要为每个操作特定端点。

I've seen some articles (even MSDN) suggest TempData for passing data between ActionMethods. But I've seen others here say that TempData should be avoided. What's the best practices way to approach this?

Here's some code to show my situation. Note: I'm 100% sure, I'm doing this wrong. Which is why I'm here. :) Also, I've been doing Webforms up until recently.

Note2: This is related, but not the same.

View:

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

Controller methods:

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

解决方案

It sounds like you are coupling your action methods to your end result too tightly.

I would refactor a little bit; you would have your Index Method like so:

 public ActionResult Index()
 {
      HTMLMVCCalendar.Models.MonthModel someModel = new HTMLMVCCalendar.Models.MonthModel();

      someModel.DateTime = DateTime.Now; // whatever

      return View(someModel);
 }

Then, when you need to recalculate your calendar, you simply post to the same URL which returns the same view with new view model data.

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

You stay on the same URL and present the same view, but with the changes that you need. You dont need a specific endpoint for each action.

这篇关于我应该通过RedirectToAction或TempData的路过值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆