1

我的控制器是:

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 调用),暴露了查询字符串上的所有参数,不能有复杂的模型,并且在填充路由值时没有类型检查。

有没有一种很好的方法来传输操作而不在查询字符串上暴露模型详细信息?

4

1 回答 1

2

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

因此,在您的情况下,尽管您已经执行Action2并且想要显示Action2.cshtmlMVC 将尝试与抛出此异常Action1.cshtml的您一起使用。Action2Model

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

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

public ActionResult Action2(Action2Model model)
{
     //...
     return View("Action2", model);
}
于 2013-09-22T10:24:21.017 回答