从传递动作数据到另一个动作 [英] pass data from Action To Another Action

查看:95
本文介绍了从传递动作数据到另一个动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您将如何通过RedirectAction方法传递从(GETDATE)动作到另一个(ProcessP)的行动模式?

下面是源$ C ​​$ C:

  [HttpPost]
公众的ActionResult GETDATE(的FormCollection值,DateParameter newDateParameter)
{
    如果(ModelState.IsValid)
    {
返回RedirectToAction(ProcessP);
    }
    其他
    {
返回查看(newDateParameter);
    }
}
公众的ActionResult ProcessP()
{
   //从GETDATE访问模式在这里?
    VAR模型=(从_db.blah p
 排序依据p.CreateDate下降
 选择P)。取(10);    返回查看(模型);
}


解决方案

如果您需要从一个动作的数据传递到另外一个选择是使用的 TempData的。例如在GETDATE如下可以将数据添加到会话:

 的TempData [钥匙] = YourData

,然后执行重定向。在ProcessP您可以访问使用你previously使用的密钥数据:

  VAR不管= TempData的[钥匙];

有关一个体面的阅读,我会建议通过此线程读取: ASP.NET MVC - TempData的 - 是好是坏的做法

How would you pass the model from an (GetDate) action to another (ProcessP) action via RedirectAction method?

Here's the source code:

[HttpPost]
public ActionResult GetDate(FormCollection values, DateParameter newDateParameter)
{
    if (ModelState.IsValid)
    {
return RedirectToAction("ProcessP");
    }
    else
    {
return View(newDateParameter);
    }
}


public ActionResult ProcessP()
{
   //Access the model from GetDate here??
    var model = (from p in _db.blah
 orderby p.CreateDate descending
 select p).Take(10);

    return View(model);
}

解决方案

If you need to pass data from one action to another one option is to utilize TempData. For example within GetDate you could add data to the session as follows:

TempData["Key"] = YourData

And then perform the redirect. Within ProcessP you can access the data utilizing the key you previously used:

var whatever = TempData["Key"];

For a decent read, I would recommend reading through this thread: ASP.NET MVC - TempData - Good or bad practice

这篇关于从传递动作数据到另一个动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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