pass model from one action to another action in same controller
Asked Answered
S

4

7

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");
        }

    }
Shouse answered 26/1, 2012 at 20:16 Comment(2)
Why not do return WriteInTemplate(statementList); instead? You should probably move the common functionality in another method.Greensand
@Greensand is correct. just "return WriteInTemplate(statementList);" That is also an ActionResult so it will work as desired.Fundamentalism
R
3

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

Rowena answered 23/9, 2013 at 16:1 Comment(0)
I
1

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

Indusium answered 26/1, 2012 at 21:7 Comment(0)
P
1

This is because you had spefified wrong route parameters.

while thinking about this.. did you check that the data are not null?

you are using

 return RedirectToAction("WriteInTemplate", statementList );

instead you should use

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

see reference here

Peshitta answered 30/1, 2012 at 9:15 Comment(1)
He said he was in the same controller, therefore what he has would have worked and is using correct parameters.Rowena
G
1

The way you call the RedirectToAction() method may not be your issue.

For me, the solutions presented above did not work because the RedirectToAction() method builds a RouteValueDictionary by using the .ToString() value of each property in the model. This will only work if all the properties in the model are simple properties and it fails if any properties are complex objects, lists, collections, etc. because this method does not use recursion.

If for example, a model called MymodelOrganization contained a property List Employees, then that property would result in a query string value of ....&Employees=System.Collections.Generic.List'1[System.String] and binding would fail, and you would end up (as was my case) with ... null

I had this problem, so I created a copy of my model containing only the elements of the form, stripping my Lists and passed that inside RedirectToAction(). Once on the other action method, I was able to re-assemble my Lists and added them to my Model before calling the last return. Good luck. Here is the idea in my code:

[HttpPost]
public ActionResult ItemSubmissionForm(CombinedModelContent membervalues)
{ ...
    ItemSubmissionsDBFields aFieldsList = membervalues.FieldsList;  //Stripping other objects
    return RedirectToAction("ItemSubmissionConfirm", aFieldsList);
}

[HttpGet]
public ActionResult ItemSubmissionConfirm(ItemSubmissionsDBFields aFieldsList)
{ ...
   List<SomeArea> SomeAreaitems = new List<SomeArea>();
   SomeAreaitems.Add  ...

   CombinedModelContent copymembervalues = new CombinedModelContent();
        copymembervalues.SomeCodeLists = SomeAreaitems;
        copymembervalues.FieldsList = aFieldsList;  
   return View("SomeConfirmPage", copymembervalues);
Glazunov answered 21/8, 2019 at 11:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.