Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
i'm a beginner in asp.net mvc 4 and i tryin to fill dropdownlist from database i'don't want to use entity for some raisins
plz, can anybody help me with this :

Medel :Client.cs
C#
public class Client
   {
       public int ClientID { get; set; }
       public string Nom { get; set; }

   }


controller :ClientController.cs

public ActionResult Add()
{
string cn= ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
var client = new Client
{
using (var con = new SqlConnection(cn))
{
con.Open();
using (var command = new SqlCommand("SELECT * FROM mytable", con))
{
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
string Name= reader[1] as string;
client.add(new Client() { Nom= Name});
}
}
con.Close();
}
}
return View(client);
}



veiw :
@Html.DropDownListFor("-- Select --", new SelectList(""))
Posted

1 solution

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

using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
using System.Data;
using System.Data.SqlClient;

namespace Clients.model
{
public class Class1
{
string cn= ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;

public List<client> DropClient()
{
SqlConnection db = new SqlConnection(cn);
string query = "SELECT * FROM mytable"";
SqlCommand cmd = new SqlCommand(query,db);
db.Open();

List<client> list = new List<client>();
using (IDataReader dataReader = cmd.ExecuteReader())
{
while (dataReader.Read())
{
Client obj = new Client();
if (dataReader["ClientID"] != DBNull.Value)
{
if (dataReader["ClientID "] != DBNull.Value) { obj.ClientID= (int)dataReader["ClientID"]; }
if (dataReader["Nom"] != DBNull.Value) { obj.Nom= (string)dataReader["Nom"]; }
list.Add(obj);
}
}
return list;
}
}
}

and write controller something like this,


XML
public ActionResult Create()
       {
           Class1 dbconnection = new Class1();
           List<Client> pcontent = new List<Client>();
           {
               pcontent = dbconnection.DropClient();

           };
           List<SelectListItem> clientList= new List<SelectListItem>();
           //List<string> items = new List<string>();
           foreach (var item in pcontent)
           {
               clientList.Add(new SelectListItem
               {
                   Text = item.No,
                   Value = item.ClientID.ToString()
               });
           }

           ViewBag.clientList= clientList;
           return View();
       }



and create a view

@Html.DropDownList("Clients", (IEnumerable<SelectListItem>)ViewBag.clientList,"-- Select --")



hope this will help you
 
Share this answer
 
v5

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900