1

I have one view that used to display the result of my search item. This is its controller :

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

I displayed the content in the view by looping ViewData["EmployeeType"] and I also included one Print label :

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

I used function printPage('/Report/PrintEvaluatingReport') to load the print dialog with the action PrintEvaluatingReport. So I want to take the object that I have in EvaluatingReport to the action PrintEvaluatingReport.

Could anyone tell me how could I do that?

Thanks in advanced.

1
  • 1
    I would highly suggest looking into strongly typed views and using a ViewModel for your results. Commented Aug 11, 2012 at 1:40

2 Answers 2

1

You could use 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();
}
Sign up to request clarification or add additional context in comments.

Comments

1

You can use a session variable for that !!

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

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.