Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET .NET MVC MVC3
Hi I am new to MVC and want to know how to bind dropdownlist from list and from database.
 
I need simple examples. A very simple Examples.
Thanks
Posted 24-May-12 1:00am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

drop.cshtml Code
 
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
    var listItems = new List<SelectListItem>();
    listItems.Add(new SelectListItem { Text = String.Empty, Value = String.Empty });
    foreach (var name in ViewBag.Names)
    {
        listItems.Add(new SelectListItem { Text = name, Value = name });
    }
}
<h2>
    Index</h2>
@Html.DropDownList("dropDownList", listItems)
 
Controller:
 
public ActionResult drop()
      {
          List<string> abc=new List<string>(2);
          var q = from a in objContext.Details
                  select a;
          foreach (var i in q)
          {
              abc.Add(i.Name);
          }
          ViewBag.Names = abc;
          return View(q);
      }
  Permalink  
Comments
member60 at 25-May-12 2:22am
   
my 5!
Kaushal Parekh at 22-Mar-13 9:21am
   
That very nice explanation. But somewhat problem is still unsolved.
I need full code in VB.
At the line : listItems.Add(new SelectListItem { Text = String.Empty, Value = String.Empty });
 
I couldn't be able to convert it into VB.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

  Permalink  
Comments
MP Prajapati at 23-Feb-13 2:44am
   
How to bind textfiled and valuefield data in dropdowbliast from database?
Avik Ghosh22 at 5-Mar-13 0:30am
   
thank u bro...good link...5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 6,055
1 DamithSL 4,621
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,500
4 Sergey Alexandrovich Kryukov 3,294


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 27 May 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100