通过直接调用Action方法进行传输?

时间:2013-09-22 10:15:42

标签: asp.net-mvc asp.net-mvc-4

我的控制器是:

public ActionResult Action1(Action1Model model)
{
   .....
   if (...)
      return Action2(new Action2Model() { .... } );  //**
   else
      return View(model);
}

public ActionResult Action2(Action2Model model)
{ ... }

基本上,在Action1的某些条件下,我想将处理转移到Action2。上面的代码给出了一个错误:The model item passed into the dictionary is of type 'Action2Model', but this dictionary requires a model item of type 'Action1Model'

我可以在**行使用它来使它工作:

return RedirectToAction("Action2", new { parm1 = ..., parm2 = ... ...});

但是这种方法返回302(额外的Http调用),公开查询字符串上的所有参数,不能有复杂的模型,并且在填充路径值时没有类型检查。

有没有一种很好的方法来传输操作而不在查询字符串上公开模型细节?

1 个答案:

答案 0 :(得分:2)

如果在调用View时未指定视图名称,ASP.NET MVC会尝试根据原始操作名称查找视图。

因此,在您的情况下,虽然您已执行Action2并且您想要显示Action2.cshtml MVC,但会尝试将Action1.cshtmlAction2Model一起使用,这会引发此异常。< / p>

您可以通过在操作中明确写出视图名称来解决此问题:

public ActionResult Action1(Action1Model model)
{
   //....
   if (...)
      return Action2(new Action2Model() { .... } );  //**
   else
      return View("Action1", model);
}

public ActionResult Action2(Action2Model model)
{
     //...
     return View("Action2", model);
}
相关问题