MVC ActionResult calls another ActionResult

I have an ActionResult calling another ActionResult.

I have an ActionResult call in my case that does not work. Here is what I have:

public ActionResult GetReport(string pNum) { .... switch (methodId) { case 1: case 5: { var actionResult = GetP1Report("33996",false) as ActionResult; break; } } return actionResult; } 

I get the following error: "actionResult" does not exist in the current context

If I do the following, this works, but not quite what I need:

  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; } 

How to get actionResult to work in my case statement so that it is visible when I do

  return actionResult 
+6
source share
2 answers

Just declare it first (with default value, I think), outside the switch statement:

  ActionResult actionResult = null; switch (methodId) { case 1: case 5: // PVT, PVT-WMT { actionResult = GetP1Report("33996",false) as ActionResult; break; } } return actionResult ?? new View(); 

Note. I added value ?? new View() ?? new View() as the default value, if none of the cases assigned something to actionResult - change this if necessary.

+8
source

The problem is a variable area. dbaseman pretty much everything is right ... do this:

 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; } 
0
source

All Articles