Populate (Bind) DropDownList in ASP.Net MVC.

0
(0)

Populate (Bind) DropDownList in ASP.Net MVC.

Introduction
Different ways to bind the Dropdown list in ASP.Net MVC.
This article presents a couple of common ways to populate dropdown lists in ASP.NET MVC Razor views,

 

Objective
Various ways to bind the dropdown list in MVC
Capture selected Dropdown value on Postback in MVC

 

Instructions

Method 1: Binding dropdown using viewdata

public ActionResult BindRecords(){
  List<SelectListItem> accessories = new List<SelectListItem>();
  accessories.Add(new SelectListItem{
    Text = "Keyboard",
    Value = "1"
  });
  accessories.Add(new SelectListItem{
    Text = "Mouse",
    Value = "2"
  });
  accessories.Add(new SelectListItem{
    Text = "Monitor",
    Value = "3"
  });
  ViewData["accessories"] = accessories;
  return View();
}

Binding Dropdown with viewdata
@Html.DropDownList("Accessories", (IEnumerable<SelectListItem>)ViewData["accessories"])%>
Or
@Html.DropDownList("Accessories",ViewData["accessories"] as List<SelectListItem>)

 

Method 2: Binding dropdown using Model

Public class AccessoriesModel{
  public List<SelectListItem> accessories {get; set}
  public int accessoryId {get; set}
}

public ActionResult BindRecords(){
  List<SelectListItem> _accessories = new List<SelectListItem>();
  _accessories.Add(new SelectListItem{
    Text = "Keyboard",
    Value = "1"
  });
  _accessories.Add(new SelectListItem{
    Text = "Mouse",
    Value = "2"
  });
  _accessories.Add(new SelectListItem{
    Text = "Monitor",
    Value = "3"
  });

  var model = new AccessoriesModel(){
    accessories = _accessories,
    accessoryId = 1
  };
  return View(model);
}

Binding Dropdown with viewdata
Create strongly-typed view with model reference

@model BindingDropdown.Models.AccessoriesModel
@Html.DropDownList("Accessories", Model.accessories)
OR
@Html.DropDownListFor(M => M.accessories, new SelectList(Model.accessories,"Value", "Text"))
OR
@Html.DropDownListFor(m => m.accessoryId, Model.accessories, "Please select")

 

Method 3: Binding dropdown using ViewBag

public ActionResult BindRecords(){
  List<SelectListItem> accessories = new List<SelectListItem>();
  accessories.Add(new SelectListItem{
    Text = "Keyboard",
    Value = "1"
  });
  accessories.Add(new SelectListItem{
    Text = "Mouse",
    Value = "2"
  });
  accessories.Add(new SelectListItem{
    Text = "Monitor",
    Value = "3"
  });
  ViewBag.accessories = accessories;
  return View();
}
Binding Dropdown with viewdata
@Html.DropDownList("Accessories", ViewBag.accessories as SelectList)

 

Method 4: Binding dropdown using Static values

@Html.DropDownList("Accessories", new List<SelectListItem>
{
  new SelectListItem { Text = "Keyboard", Value = "1", Selected=true},
  new SelectListItem { Text = "Mouse", Value = "2"},
  new SelectListItem { Text = "Monitor", Value = "3"}
}, "Select Accessory")

 

 

More on Asp.Net …

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

Be the first to comment

Leave a Reply

Your email address will not be published.


*


six + three =