我有一个ActionResult调用另一个ActionResult。
我在我的case语句中调用了一个ActionResult,但是没有用。这就是我所拥有的:
public ActionResult GetReport(string pNum)
{
....
switch (methodId)
{
case 1:
case 5:
{
var actionResult = GetP1Report("33996",false) as ActionResult;
break;
}
}
return actionResult;
}
我收到以下错误:'actionResult'在当前上下文中不存在
如果我执行以下操作,它可以工作但不完全符合我的要求:
public ActionResult GetReport(string pNum)
{
....
var actionResult = GetP1Report("33996",false) as ActionResult;
switch (methodId)
{
case 1:
case 5:
{
// var actionResult = GetP1Report("33996",false) as ActionResult;
break;
}
}
return actionResult;
}
如何让actionResult在我的case语句中工作,以便在我这样做时可见
return actionResult
答案 0 :(得分:8)
在switch语句之外首先声明它(我猜是默认值):
ActionResult actionResult = null;
switch (methodId)
{
case 1:
case 5: // PVT, PVT-WMT
{
actionResult = GetP1Report("33996",false) as ActionResult;
break;
}
}
return actionResult ?? new View();
注意:我添加了?? new View()
作为默认值,以防所有案例都没有向actionResult
分配任何内容 - 根据需要进行修改。
答案 1 :(得分:0)
问题是可变范围。 dbaseman几乎做对了......做到这一点:
public ActionResult GetReport(string pNum)
{
....
ActionResult actionResult = new View(); // This would typically be assigned a
// default ActionResult
switch (methodId)
{
case 1:
case 5:
{
actionResult = GetP1Report("33996",false) as ActionResult;
break;
}
}
return actionResult;
}