All about Programming and Sourcecode

How to Populate the DropDownList in ASP.NET MVC Razor View?

The DropDownList is one of the most useful controls that can be used in various scenarios that involves selecting the data from the list.

The ASP.NET MVC Framework provides the Html.DropDownList and Html.DropDownListFor helper methods which can be used to create DropDownList.

How to Populate DropDownList in ASP.NET MVC Razor View?

Below is a sample source code demonstrating how to Populate DropDownList in ASP.NET MVC Razor View.

View

@{

if (@ViewContext.ViewData.ModelState.IsValid)

{

ViewBag.Title = "Index";

}

}

<h2>Abundant Code</h2>

@Html.DropDownList(“StateType”)

Controller

using MvcApplication1.Models;

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

namespace MvcApplication1.Controllers

{

public class EmployeeController : Controller

{

public ActionResult Index()

{

ViewBag.ApplicationTitle = "AbundantCode App";

List<SelectListItem> obj = new List<SelectListItem>();

obj.Add(new SelectListItem { Text = "Solid State", Value = "1" });

obj.Add(new SelectListItem { Text = "Liquid State", Value = "2" });

obj.Add(new SelectListItem { Text = "Gas State", Value = "3" });

ViewBag.StateType = obj;

return View();

}

}

}

How to Populate the DropDownList in ASP.NET MVC Razor View?

0saves
If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.
 
Comments

is it possible to make “Solid State”, “Liquid State”, “Gas State” as hyperlinks

Here is one more alternate simple solution for this problem:
In Model
1. Firstly create model class named Course and create property.
2. Then create another model class named as u wish. Here we use connection string and stored procedure.but i am using dll class here for connectivity.

In Controller and view we need to implement it this way:
http://www.mindstick.com/blog/607/Bind%20Dropdown%20in%20MVC

Leave a Reply