如何在不使用Entity的情况下从数据库填充dropdownlist [英] how to fill dropdownlist from database without using Entity

查看:71
本文介绍了如何在不使用Entity的情况下从数据库填充dropdownlist的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是asp.net mvc 4的初学者,我试着填写数据库中的dropdownlist我不想用某些葡萄干的实体

plz,任何人都可以帮我这个:



Medel:Client.cs

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

public class Client
   {
       public int ClientID { get; set; }
       public string Nom { get; set; }

   }





控制器:ClientController.cs



public ActionResult Add()

{

string cn = ConfigurationManager.ConnectionStrings [DefaultConnection]。ConnectionString;

var client =新客户

{

使用(var con = new SqlConnection(cn))

{

con。打开();

使用(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。关闭();
}

}

返回查看(客户端);

}







veiw:

@ Html.DropDownListFor( - 选择 - ,新选择列表( ))



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(""))

推荐答案

使用System;

使用System.Collections.Generic;

使用System.Linq;

使用System.Web;

使用System.Collections;



使用System.ComponentModel.DataAnnotations;

使用System.Web.Mvc;

使用System.Data;

使用System.Data.SqlClient;



命名空间Clients.model

{

公共类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())

{

客户端obj =新客户端();

if(dataReader [ClientID]!= DBNull。价值)

{

if(dataReader [ClientID]!= DBNull.Value){obj.ClientID =(int)dataReader [ClientID];}

if(dataReader [Nom]!= DBNull.Value){obj.Nom =(string)dataReader [Nom];}

list.Add(obj );
}

}

返回列表;

}

}

}



并写这样的控制器,





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,


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();
       }







并创建一个视图



@ Html.DropDownList(客户,(IEnumerable< SelectListItem>)ViewBag.clientList, - 选择 - )



< br $> b $ b

希望这能帮到你




and create a view

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



hope this will help you


这篇关于如何在不使用Entity的情况下从数据库填充dropdownlist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆