如何将数据集从一个动作方法传递到另一个动作方法? [英] How to pass dataset from one actionmethod to another?

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

问题描述

public ActionResult Admin(ObjectModel OM)

             {             

        DataSet ds = new DataSet();   
        DB objDB = new DB();    
           //Storing Dataset in model Dataset    

              OM.StoreAllData = objDB.SelectAllDetails(OM.DistrictId);  

           //Need to pass this Dataset into "Show" Method

  return RedirectToAction("show", "Appform",new  { ds = OM.StoreAllData });  


           }


 public ActionResult show(DataSet ds)  
 {      
     return View(OM);    
 }





我的尝试:



i已尝试上述代码。我无法弄清楚如何将数据集传递给两个ActionMethod



What I have tried:

i have tried the above code. I am not able to figure out how to pass the dataset amoung two ActionMethod

推荐答案

将您的数据集对象传递给Tempdata然后将该Tempdata对象转换为其他操作方法。像这样。



Pass your data set object to Tempdata and Then get that Tempdata object to other action method. Like this.

//This code put in your first action method.
   TempData["DataSet"] = ds;

// And get like this in your second action method.

   DataSet ds = TempData["DataSet"] as DataSet ;





TempData: -

TempData是一个字典对象,派生自TempDataDictionary类并存储在短期会话中。



TempData:-
TempData is a dictionary object that is derived from TempDataDictionary class and stored in short lives session.

public TempDataDictionary TempData { get; set; }



TempData是ControllerBase类的属性。

TempData用于将数据从当前请求传递到后续请求(意味着从一个页面重定向另一个)。

它的生命非常短暂,直到目标视图完全加载为止。

需要进行类型转换以获取数据并检查空值以避免错误。

它只用于存储错误消息,验证消息等一次性消息。要使用TempData保存数据,请参阅以下文章:使用TempData保存数据


TempData is a property of ControllerBase class.
TempData is used to pass data from current request to subsequent request (means redirecting from one page to another).
It’s life is very short and lies only till the target view is fully loaded.
It’s required typecasting for getting data and check for null values to avoid error.
It is used to store only one time messages like error messages, validation messages. To persist data with TempData refer this article: Persisting Data with TempData


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

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