[英]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. 我试图将我的模型List <Models.Statement> statementList从一个动作传递到另一个动作,但我在第二个控制器中接收空值。 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");
}
}
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 . 你没有传递“statementList”,而是传递新的{statementList = statementList}只是传递模型,你应该没问题。
return RedirectToAction("WriteInTemplate", statementList);
Answer by sino 回答sino
RedirectToAction()
writes a redirect command to the browser, making it start a brand new request to WriteInTemplate()
. RedirectToAction()
将重定向命令写入浏览器,使其开始向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? WriteInTemplate()
是一个独立的操作,它有时会负责来自用户的整个请求或来自视图的部分请求吗? If not, you should just call it as a regular method instead of using RedirectToAction()
. 如果没有,您应该将其称为常规方法,而不是使用
RedirectToAction()
。
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});
The way you call the RedirectToAction() method may not be your issue. 您调用RedirectToAction()方法的方式可能不是您的问题。
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. 对我来说,上面提到的解决方案不起作用,因为RedirectToAction()方法通过使用模型中每个属性的.ToString()值来构建RouteValueDictionary。 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 例如,如果名为MymodelOrganization的模型包含属性List Employees,那么该属性将导致查询字符串值为....&Employees = System.Collections.Generic.List'1 [System.String]并且绑定将失败,你最终会得到(就像我的情况一样)......
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(). 我有这个问题,所以我创建了我的模型的副本,只包含表单的元素,剥离我的列表并在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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.