首页 > 解决方案 > 当我从控制器返回 mvc 中的视图时出错

问题描述

我的控制器中有一个方法,它将一个布尔变量作为可选参数(这应该是可选的,不是强制性的),尽管当我返回视图时它说视图需要一个参数。

我有我的模型视图,它将 bool 设置为 true 或 false,以便在确实发生某个操作时在视图中显示错误消息。

这是我的代码

    public ActionResult Index([Optional] bool cameFromModal)
    {cameFromModal = false;
         if (cameFromModal == true)
              {
            vm.Error = true; // I set the bool to the model so that I can                       display an error message on my returned view
              }


        return View(vm);
    }

标签: asp.net-mvcmodel-view-controlleroptional-parameters

解决方案


也许你可以试试这个;

public ActionResult Index(bool cameFromModal=false)
{             
if (cameFromModal == true)
{
vm.Error = true;
}
return View(vm);
}

当您触发 true 时,它​​会出错


推荐阅读