From one action to another in one controller

I am trying to pass my List <Models.Statement> statementList model from one action to another, but I get a null value in the second controller. Please suggest what is wrong here. Even 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"); } } 
+7
source share
4 answers

Please look here to pass your model.

you don’t pass the "statementList", instead you pass the new {statementList = statementList}, just pass the model and you should be fine.

 return RedirectToAction("WriteInTemplate", statementList); 

The answer to this question is sino

+3
source

RedirectToAction() writes the redirect command to the browser, causing it to start a new WriteInTemplate() request. Therefore, your model object is lost.

Is WriteInTemplate() independent action that will sometimes be responsible for the entire request from the user or a partial request from the view? If not, you should just call it like a regular method, and not use RedirectToAction() .

+1
source

This is due to the fact that you specified the wrong route parameters.

thinking about it. Have you verified that the data is not null?

you are using

  return RedirectToAction("WriteInTemplate", statementList ); 

you should use instead

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

see link here

+1
source

How you call the RedirectToAction () method may not matter.

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

If, for example, a model named MymodelOrganization contains the List Employees property, then this property will result in the value of the query string .... & amp; Servants = System.Collections.Generic.List'1 [System.String] and the binding will fail, and you will get (as in my case) ... null

I had this problem, so I made a copy of my model containing only form elements, deleted my lists and passed it inside RedirectToAction (). Once in a different method of action, I was able to rebuild my lists and add 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); 
0
source

All Articles