2012-08-11 54 views
1

我有一個視圖用於顯示我的搜索項目的結果。這是它的控制器:將對象傳遞給ASP.net的另一個動作MVC 2

[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

有誰能告訴我我該怎麼做?

謝謝先進。

+1

我會強烈建議尋找到強類型的意見,並使用一個ViewModel你的結果。 – 2012-08-11 01:40:38

回答

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(); 
} 
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; 
    . 
    . 
    . 

} 
相關問題