Can we use Nested switch case in C# or switch case under if else case in ASP.NET C#?


Hello,Can we use Nested switch case in C# or switch case under if else case in ASP.NET C#? If yes, can anyone provide an example? I have been successfully able to redirect users to certain pages depending on the basis of the ratings. Now I want to redirect them depending on the basis of the ratings as well as DestinationID. I have attached the code for redirecting users depending on the ratings done by them.

[HttpPost]
public ActionResult Register(FormCollection collection) //UserRating ratingModel
{


int rating = Convert.ToInt32(collection["Rating"]);

switch (rating)
{
case 1:

return RedirectToAction("RecommendationPlaces");
break;
case 2:

return RedirectToAction("RecommendationPlaces2");
break;
case 3:

return RedirectToAction("RecommendationPlaces3");
break;
case 4:

return RedirectToAction("RecommendationPlaces4");
break;
case 5:

return RedirectToAction("RecommendationPlaces5");
case 6:

return RedirectToAction("RecommendationPlaces6");
case 7:

return RedirectToAction("RecommendationPlaces7");
case 8:

return RedirectToAction("RecommendationPlaces8");
case 9:

return RedirectToAction("RecommendationPlaces9");
case 10:

return RedirectToAction("RecommendationPlaces10");
default:

return RedirectToAction("RecommendationPlaces11");


}

Asked by:- DixonCh
1
: 3656 At:- 1/8/2018 5:49:10 AM
ASP.NET ASP.NET MVC nested-switch-case-c# if-else-inside-switch-case







1 Answers
profileImage Answered by:- vikas_jk

Hello, you can use nested switch case for better output or you can also use Nested-If, here is the general C# syntax of nested Switch

switch(ID1) {
   case 'A':
   Console.WriteLine("This A is part of outer switch" );
   
   switch(ID2) {
      case 'A':
         Console.WriteLine("This A is part of inner switch" );
         break;
      case 'B': /* inner B case code */
   }
   break;
   case 'B': /* outer B case code */
}

So, in your controller code you can have code something like this

public ActionResult Index()
        {
            int rating = 1;
            int DestinationId = 2;

            switch (rating)
            {
                case 1:
                    switch (DestinationId)
                    {
                        case 2:
                           //this will redirect to action Case2 directly and no other options will be executed
                            return RedirectToAction("Case2");

                    }
                    break;
                   
                    
                case 2:
                    switch (DestinationId)
                    {
                        case 3:
                            return RedirectToAction("Case3");

                    }
                    break;
                    
            }
                    return View();
        }

Let me know if it resolves your query, you can comment on the answer, thanks

2
At:- 1/8/2018 7:24:56 AM
Thank you so much vikas_jk bro! It worked perfectly as I had expected. I must appreciate your talent! I wanted to give more than one vote to your answer but there is 1 vote per person so I'm compelled to stay at 1 vote for you. Thanks again!! 1
By : DixonCh - at :- 1/8/2018 11:03:46 AM






Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use