将对象传递给ASP.net MVC 2的另一个动作

时间:2012-08-11 01:34:28

标签: asp.net-mvc

我有一个用于显示搜索项结果的视图。这是它的控制器:

[HttpPost]
public ActionResult EvaluatingReport(FormCollection frm)
{
 string empType = frm["empType"].ToString();
 int totalMonth = 3;
 int mon = DateTime.Now.Month;
 int yr = DateTime.Now.Year;
 string Curdate = mon + "/" + yr;
 DateTime date = new DateTime();
 var result = (from e in context.tblEmployees
 join p in context.tblEmployee_Position on e.PositionIDF equals p.PositionID
 select new EvaluateEmployee
  {
    ID = e.Code,
    IDCard = e.IDCard,
    Name = e.NameEng,
    DOB = e.DOB,
    Position = p.Position,
    Sex = e.Sex,
    StartDate = e.StartDate
}).ToList();
  result = result.Where(((s => mon - int.Parse(s.StartDate.Substring(3, 2).ToString()) == totalMonth && yr -int.Parse(s.StartDate.Substring(6, 4).ToString()) ==  totalYear))).ToList();
  ViewData["EmployeeType"] = result;
  return View();
}

我通过循环ViewData["EmployeeType"]在视图中显示了内容,并且还添加了一个Print标签:

<script language="javascript" type="text/javascript">
  function printChart() {
    var URL = "EvaluatingReport";
    var W = window.open(URL);
    W.window.print(); 
  }
  function printPage(sURL) {
    var oHiddFrame = document.createElement("iframe");
    oHiddFrame.src = sURL;
    oHiddFrame.style.visibility = "hidden";
    oHiddFrame.style.position = "fixed";
    oHiddFrame.style.right = "0";
    oHiddFrame.style.bottom = "0";
    document.body.appendChild(oHiddFrame);
    oHiddFrame.contentWindow.onload = oHiddFrame.contentWindow.print;
    oHiddFrame.contentWindow.onafterprint = function () {          
    document.body.removeChild(oHiddFrame); };
  }   
  </script> 
  <span onclick="printPage('/Report/PrintEvaluatingReport');" style="cursor:pointer;text-decoration:none;color:#0000ff;"> 
     <img src="../../Content/images/Print.png" width="35px;" alt="print" style="position:relative;top:6px;"/> 
      &nbsp; Print Report
  </span>

我使用函数printPage('/Report/PrintEvaluatingReport')加载了包含操作PrintEvaluatingReport的打印对话框。所以我想把EvaluatingReport中的对象带到动作PrintEvaluatingReport

有谁能告诉我怎么能这样做?

先谢谢。

2 个答案:

答案 0 :(得分:1)

您可以使用会话变量!!

private EmployeeType Result
{
    get { return (EmployeeType)this.Session["Result"]; }
    set
    {
        this.Session["Result"] = value;
    }
}

[HttpPost]
public ActionResult EvaluatingReport(FormCollection frm)
{
   .
   .
   .    
   // Store in Session
   this.Result = result;

   return View();
}

public ActionResult PrintEvaluatingReport()
{
   var employeeType = this.Result;
   .
   .
   .

}

答案 1 :(得分:1)

您可以使用TempData["EmployeeType"]

[HttpPost]
public ActionResult EvaluatingReport(FormCollection frm)
{
    string empType = frm["empType"].ToString();
    int totalMonth = 3;
    int mon = DateTime.Now.Month;
    int yr = DateTime.Now.Year;
    string Curdate = mon + "/" + yr;
    DateTime date = new DateTime();
    var result = (from e in context.tblEmployees
    join p in context.tblEmployee_Position on e.PositionIDF equals p.PositionID
    select new EvaluateEmployee
    {
       ID = e.Code,
       IDCard = e.IDCard,
       Name = e.NameEng,
       DOB = e.DOB,
       Position = p.Position,
       Sex = e.Sex,
       StartDate = e.StartDate
   }).ToList();
   result = result.Where(((s => mon - int.Parse(s.StartDate.Substring(3, 2).ToString()) == totalMonth && yr -int.Parse(s.StartDate.Substring(6, 4).ToString()) ==  totalYear))).ToList();
   ViewData["EmployeeType"] = result;


   // Store in TempData to be read by PrintEvaluatingReport
   TempData["EmployeeType"] = result;

   return View();
}

public ActionResult PrintEvaluatingReport()
{
   var employeeType = TempData["EmployeeType"] as EmployeeType;
   // do stuff
   return View();
}