problem in dropdown binding from db

i can bind the drop down from db.But I want to add please select in drop down list  as the first list item with value 0.I cannot do  so.please help

in my view

<%= Html.DropDownList("lstProducts", new SelectList((IEnumerable)ViewData["ProductList"], "regionid", "RegionDescription"))%>

in my controler i have written

public ActionResult Reg()
        {
          

            var products = from prod in obj.Regions
                           select prod;

            ViewData["ProductList"] = products;
           // ViewData["lstProducts"] = "";

            return View();
         
        }

In your SP write

SELECt ‘ — Select Region– ‘ AS regionid, 0 AS RegionDescription
Union
SELECt regionid, RegionDescription

Or

obj.Regions.InsertAt(0,’Select’)

Hi,

look at the below url :

http://forums.asp.net/t/1554948.aspx

Hi,

use like this……………

private void fillpaper()
    {
        con.Open();
        SqlCommand com = new SqlCommand(comndstr,con);        
        Int32 i = 1;
        ddlPPaper.Items.Clear();
        ddlPPaper.Items.Add("-Select Paper-");
        ddlPPaper.Items[0].Value = "0000";       
        dr = com.ExecuteReader();
        while (dr.Read())
        {
            ddlPPaper.Items.Add(dr[2].ToString());
            ddlPPaper.Items[i].Value = dr[0].ToString();
            i = i + 1;
        }
        dr.Close();
        con.Close();
    }

Thanks…

Hi,

Try using the following method:

<%=Html.DropDownList("product", ViewData["ProductList"] as IEnumerable<SelectListItem>,"please select in drop down list  ")%>
public ActionResult Reg()
        {
           

            var products = from prod in obj.Regions
                           select new SelectItem
                            {
                               Text=prod.xx,
                               Value=prod.Id
                             }.ToList();

            ViewData["ProductList"] = products;
            

            return View();
          
        }

 

Hi,

If you want to just display ‘Select’ as label in dropdown, you dont have to do anything, just add as beow:

@Html.DropDownListFor(model => model.FileSizeInUnits, new SelectList(ViewBag.FileSizeUnits, "Value", "Text"), "Select"})

Leave a Reply