Skip to main content

Bind DropDownList in Asp.MVC using ViewData object

In our previous article we have seen how to bind ViewBag object to dropdown list, In this article we will see how to bind ViewData data to dropdown. Remember we can set any kind of data to ViewData like from database, from enum, from range, fixed value etc.

Here we will take some fixed Work status list and save it to the ViewData and then in view we will bind ViewData object value to the dropdown as shown in below code snippet.

Controller code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MCVApp.Controllers
{
	public class HomeController : Controller
	{
		public ActionResult Index()
		{
			List<SelectListItem> taskStatus = new List<SelectListItem>();
			taskStatus.Add( new SelectListItem { Text = "New", Value = "0" });
			taskStatus.Add(new SelectListItem { Text = "InProcess", Value = "1" });
			taskStatus.Add(new SelectListItem { Text = "OnHold", Value = "2" });
			taskStatus.Add(new SelectListItem { Text = "Complete", Value = "3" });
			taskStatus.Add(new SelectListItem { Text = "Closed", Value = "4" });
			ViewData["taskStatusList"] = taskStatus;
			return View();
		}
	}
}

Razor View code

@using System.Web.Mvc
@using System.Collections

@{
	ViewBag.Title = "Home Page";
}

@Html.DropDownList("taskStatus",
new SelectList((IEnumerable)ViewData["taskStatusList"], "Value", "Text"))

Above code will produce output as below image.

You can see in the controller we have created a list of SelectListItem object, and then added 1 by 1 value to list. After adding all the required items to list, we have assigned it to the ViewData object, which we have used in the razor view while rednering dropdown control.
In the view we have first cast ViewData to IEnumerable object and then again cast it to SelectList, and then pass field name of select list to display it into the dropdown.
So we have seen 3 different ways to bind a dropdown control in Asp.net MVC

You can download the full working code from here

 

Bind DropDownList in Asp.MVC using ViewBag object

In our previous article we have seen how to bind fixed value from view and how to bind Range to dropdown list, In this article we will see how to bind ViewBag data to dropdown. Remember we can set any data to ViewBag like from database, from enum, from range, fixed value etc.

Here we will take some fixed country list and save it to the ViewBag and then in view we will bind ViewBag value to the dropdown as shown in below code snippet.

Controller code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MCVApp.Controllers
{
	public class HomeController : Controller
	{
		public ActionResult Index()
		{
			List&lt;SelectListItem&gt; countryList = new List&lt;SelectListItem&gt;();
			countryList.Add( new SelectListItem { Text = "India", Value = "IN" });
			countryList.Add(new SelectListItem { Text = "United States", Value = "US" });
			countryList.Add(new SelectListItem { Text = "United Kingdom", Value = "UK" });
			countryList.Add(new SelectListItem { Text = "France", Value = "FR" });
			ViewBag.CountryList = countryList;
			return View();
		}
	}
}

Razor View code

@using System.Web.Mvc

@{
	ViewBag.Title = "Home Page";
}



@Html.DropDownList("Country", 
ViewBag.CountryList as IEnumerable&lt;SelectListItem&gt;)

Above code will produce output as below image.

Dropdown

In the controller if you see we have created a list of SelectListItem object, and then added 1 by 1 value to the list. After adding all the required items to list, we have assigned it to the ViewBag object, In razor view we have used that ViewBag data for binding a dropdown control.

In this article we have seen how to bind dropdown using ViewBag value, In our next article we will see how to bind a dropdown with ViewData object.

Bind DropDownList in Asp.MVC using Range/Fixed value

In most of the application we are using dropdown to select some value from predefined list of value. Some of them are getting bind from the database table, some of are getting bind using Enum value while some of them are getting bind using some fix value like 1 to 10, or month drop-down 1 to 12. In this article post we will see how to bind drop-down using range value in MVC.

Below if the sample code which will bind 2000 to 2014 in year drop-down.

@using System.Web.Mvc
@{
    ViewBag.Title = "Home Page";
}

@Html.DropDownList("Year",
    Enumerable.Range(2000, 15).
    Select(i =&gt; new
    SelectListItem
    {
        Text = i.ToString(),
        Value = i.ToString()
    }),new { style="width:300px;"})

above code will produce output as below image.

In the above code we have just write a code in view only, there is no server side code. Now in some cases we need to bind some data which are not in particular range like “2000, 2002, 2004, 2006, 2008” or some string type fixed data then we can not use “Enumerable.Range”, so how can we bind those kind of data. See the below example to bind fixed data to dropdown control using Asp.net MVC.

@using System.Web.Mvc
@{
	ViewBag.Title = "Home Page";
}

<br /><br />
@Html.DropDownList("Year",
	new SelectList(
	new[]
	{	
		2000, 2002, 2004, 2006, 2008,
		2010, 2012, 2015, 2018, 2022,
		2023, 2034, 2035, 2036, 2041, }),
	new { style = "width:300px;" })

above code will produce output as below image.

In this article we have seen two options to bind dropdown using Range and fixed value, In our next article we will see how to bind a dropdown with ViewBag object.