Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC ActionResult calling another ActionResult

I have an ActionResult calling another ActionResult.

I have a call to an ActionResult in my case statement which doesn't 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 it 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 do I get the actionResult to work in my case statement such that it is visible when I do

    return actionResult
like image 525
Nate Pet Avatar asked Dec 20 '12 22:12

Nate Pet


1 Answers

Just declare it first (with a default value, I guess), outside of 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 the ?? new View() as a default value, in case none of the cases assign anything to actionResult -- modify this as needed.

like image 78
McGarnagle Avatar answered Sep 28 '22 07:09

McGarnagle