Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pass model from one action to another action in same controller

I am trying to pass my model List< Models.Statement > statementList from one action to another but i am receiving null value in the 2nd controller. Please suggest what is wrong here. Even tried with:

return RedirectToAction("WriteInTemplate", new { statementList = statementList });

Please help.

    public ActionResult SendPdfStatement(string InvoiceNumber)
    {
        try
        {
            InvoiceNumber = InvoiceNumber.Trim();

            ObjectParameter[] parameters = new ObjectParameter[1];
            parameters[0] = new ObjectParameter("InvoiceNumber", InvoiceNumber);

            List<Models.Statement> statementList = new List<Models.Statement>();
            statementList = _db.ExecuteFunction<Models.Statement>("uspInvoiceStatement", parameters).ToList<Models.Statement>();

            //WriteInTemplate(statementList);
            return RedirectToAction("WriteInTemplate", statementList );

        }
        catch (Exception e)
        {
            InvoiceSearchTool.Models.udtExceptionTable exception = new udtExceptionTable();
            exception.MethodName = "SendPdfStatement";
            exception.Exception = e.ToString();
            exception.Date = DateTime.Now;
            DYNAMICS_EXTEntities db = new DYNAMICS_EXTEntities();
            db.AddToudtExceptionTables(exception);
            db.SaveChanges();  
            return View("Error");
        }
    }

    public ActionResult WriteInTemplate(List<Models.Statement> statementList)
    {
        try
        {
            string invoiceNumber = statementList.FirstOrDefault().Invoice.ToString().Trim();
        ...................snip..........


            return RedirectToAction("CreateMessageWithAttachment", "email", invoiceNumber); 
        }
        catch (Exception e)
        {
            InvoiceSearchTool.Models.udtExceptionTable exception = new udtExceptionTable();
            exception.MethodName = "WriteInTemplate";
            exception.Exception = e.ToString();
            exception.Date = DateTime.Now;
            DYNAMICS_EXTEntities db = new DYNAMICS_EXTEntities();
            db.AddToudtExceptionTables(exception);
            db.SaveChanges();

            return View("Error");
        }

    }
like image 599
14578446 Avatar asked Jan 26 '12 20:01

14578446


2 Answers

Please take a look here to pass your Model

you are not passing "statementList" , instead you are passing new { statementList= statementList} just pass the model and you should be fine .

return RedirectToAction("WriteInTemplate", statementList);

Answer by sino

like image 114
Don Thomas Boyle Avatar answered Oct 04 '22 17:10

Don Thomas Boyle


RedirectToAction() writes a redirect command to the browser, making it start a brand new request to WriteInTemplate(). Your model object is therefore lost.

Is WriteInTemplate() an independent action which will sometimes be responsible for an entire request from a user or a partial request from a view? If not, you should just call it as a regular method instead of using RedirectToAction().

like image 40
Steve Wilkes Avatar answered Oct 04 '22 18:10

Steve Wilkes