问题描述
我的控制器中有一个方法(MethodA),该方法需要在同一控制器中返回另一个ActionResult
(methodB),并将某些参数作为有效负载。为此,我在MethodA末尾使用RedirectToAction(nameof(methodB),new { param1=param1,param2=param2 } )
。
当我调试时,所有参数都被加载并包含期望值。但是,当到达methodB时,这两个参数为空。
MethodA
[HttpPost]
public ActionResult Bulkimport(int id)
{
int customerId = 5;
ReturnModel returnMessage = new ReturnModel();
returnMessage.Message= "All data loaded";
returnMessage.returnModel = returnModel; //contains list with some data
return RedirectToAction(nameof(Bulkimport),new { id = customerId,model = returnMessage });
}
MethodB
[HttpGet]
public ActionResult Bulkimport(int id,MessageModel model)
{
// Do stuff,but MessageModel and id are null.
return View();
}
有人知道这里会发生什么吗? 另外:我有一些经验,在浏览器的URL中,其他参数作为查询字符串显示在接收视图中。有什么办法可以防止它?
谢谢
解决方法
更改:
return RedirectToAction(nameof(Bulkimport),new { id = customerId,model = returnMessage });
收件人
return Bulkimport( customerId,returnMessage);
````