Binding DropDown List Using Tuple In ASP MVC

Tuple is a generic static class that was added to C# 4.0 and it can hold any amount of elements, and they can be any type we want. So using tuple, we can return multiple values.One great use of tuple might be returning multiple values from a method. It provides an alternative to "ref" or "out" if you have a method that needs to return multiple new objects as part of its response.

In this article, we are going to learn how to bind drop-down list using Tuple in ASP MVC.
 
Before that, we should know wha Tuple is and what is the usage of a tuple.
 
What is Tuple? 
 
Tuple is a generic static class that was added to C# 4.0 and it can hold any amount of elements, and they can be any type we want. So using a tuple, we can return multiple values.One great use of tuple might be returning multiple values from a method. It provides an alternative to "ref" or "out" if you have a method that needs to return multiple new objects as part of its response.

A tuple allows you to combine multiple values of possibly different types into a single object without having to create a custom class. This can be useful if you want to write a method that for example returns three related values but you don't want to create a new class.
 
To know more about Tuple, please refer Here
 
Let's see the step by step process to implement Binding drop down using Tuple,
 
Step 1
 
Let's create a simple mvc project in Visual studio. To do -> go to Visual Studio -> select File -> New - > Project -> click-> a new dialog window will open
 
Select "web" in the left pane and select "ASP.NET Web Application"  main window.Then give a name for your project and click ok. Here, I have named my project as "BindingDropDownUsingTuple".
 
Now, a new dialog window will appear again. In that, select MVC and click ok.
 
Please find the below images for your references. 
 
 
 
 
 
 
Step 2
 
Once the project has been created, you can see more folders in the solution file.
 
 
 
Step 3 
 
Next, let's create a simple class under models folders. Here, I have created a class and named it as "SchoolModels".
 
 
 
 
Here's the sample code for your reference.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5.   
  6. namespace DropDownBindingUsingTuple.Models  
  7. {  
  8.     public class Departments  
  9.     {  
  10.         public int DeptId { getset; }  
  11.         public string DeptName { getset; }  
  12.     }  
  13.   
  14.     public class Students  
  15.     {  
  16.         public int StudentId { getset; }  
  17.         public string StudentName { getset; }  
  18.     }  
  19. }  
Explanation of View Model
 
Here, I have created two classes such as "Departments" and "Students". The Departments class is used to bind the Department drop-down list and Students class is used to bind the Students' drop-down list.
 
Step 4
 
Next, let's create a simple controller under Controllers folder. Here, I have created a simple controller and named it as "SchoolController". 
 
 
 
 
Step 5
 
Let's create a "Departments" and "Students" list to bind it to the drop-down list. Just add the below code in your index method.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. using DropDownBindingUsingTuble.Models;  
  7.   
  8.   
  9. namespace DropDownBindingUsingTuble.Controllers  
  10. {  
  11.     public class SchoolController : Controller  
  12.     {  
  13.         // GET: SchoolDetails  
  14.         public ActionResult Index()  
  15.         {  
  16.             List<Departments> DeptList = new List<Departments>();  //List of Departments  
  17.             List<Students> StudentList = new List<Students>();   // List of Students  
  18.             DeptList.Add(new Departments  
  19.             {  
  20.                 DeptId=1,  
  21.                 DeptName="BCA"  
  22.             }); //adding list to DeptList Object  
  23.             DeptList.Add(new Departments  
  24.             {  
  25.                 DeptId = 2,  
  26.                 DeptName = "BSC"  
  27.             });  
  28.             DeptList.Add(new Departments  
  29.             {  
  30.                 DeptId = 3,  
  31.                 DeptName = "MCA"  
  32.             });  
  33.             StudentList.Add(new Students  
  34.             {  
  35.                StudentId=1,  
  36.                StudentName="Ramesh"  
  37.             }); // adding list to StudentList Object  
  38.             StudentList.Add(new Students  
  39.             {  
  40.                 StudentId = 2,  
  41.                 StudentName = "Kannan"  
  42.             });  
  43.             StudentList.Add(new Students  
  44.             {  
  45.                 StudentId = 3,  
  46.                 StudentName = "Vimal"  
  47.             });  
  48.             List<SelectListItem> DeptListNew = new List<SelectListItem>();  // Creating SelectListItem List  
  49.             List<SelectListItem> StudentListNew = new List<SelectListItem>();  
  50.             DeptListNew = DeptList.Select(x => new SelectListItem  
  51.             {  
  52.                 Text=x.DeptName,  
  53.                 Value=x.DeptId.ToString()  
  54.             }).ToList(); //Binding DeptList to SelectListItem DeptListNew  
  55.             StudentListNew = StudentList.Select(x => new SelectListItem  
  56.             {  
  57.                 Text = x.StudentName,  
  58.                 Value = x.StudentId.ToString()  
  59.             }).ToList(); //Binding StudentList to SelectListItem StudentListNew  
  60.             var TubleList = Tuple.Create<List<SelectListItem>,List<SelectListItem>>(DeptListNew,StudentListNew); //creating Tuple and passing two select list item in that.  
  61.             return View(TubleList); //passing Tuple to view.  
  62.         }  
  63.     }  
  64. }  
Step 6
 
Next, let us create a simple View for our Index action result. To create a View, right-click Index action result -> Add View -> just click OK.
 
Please find the below image and code for your reference.

 
  1. @model Tuple<List<SelectListItem>,List<SelectListItem>>    
  2. @{    
  3.     ViewBag.Title = "Index";    
  4. }    
  5. <h2>Index</h2>    
  6. @Html.DropDownList("DeptValue",Model.Item1, "Select","")  @*Getting Tuple first list using Model.Item1*@  
  7. <br />    
  8. <br />   
  9. <br />    
  10. @Html.DropDownList("StudValue", Model.Item2, "select""")  @*Getting Tuple second list using Model.Item2*@  
Step 7
 
Run the application and see the expected result. Please find the sample result below.