use the below code, you need to fetch your data in the DataTable and bind it with the Drop Down control
DropDownList1.DataTextField = "your Coloumn Name"; // name appear on list
DropDownList1.DataValueField = "Your colum Name" // use as unique Id usally primary key
DropDownList1.DataSource = YourDataTAble;
DropDownList1.DataBind();
Hope answer the question
Html.DropDownList
Please mark as answer if this post answered or solved your problem.
DropDownList1.DataTextField = "@productName"; // name appear on list
DropDownList1.DataValueField = "@productId" // use as unique Id usally primary key
DropDownList1.DataSource = YourDataTAble;
DropDownList1.DataBind();
You can so this in one of two ways. The first uses plain HTML:
var sql = "SELECT ProductId, ProductName FROM Products";
var data = Database.Open("Northwind").Query(sql);
<select name="productid">
@foreach(var row in data){
<option value="@row.ProductId">@row.ProductName</option>
}
</select>
Or you can use the Html.DropDownList:
var sql = "SELECT ProductId, ProductName FROM Products";
var data = Database.Open("Northwind").Query(sql);
var items = data.Select(i => new SelectListItem {
Value = i.ProductId.ToString(),
Text = i.ProductName
});
@Html.DropDownList("productid", items)
The
Html.DropDownList helper expects an IEnumerable<SelectListItem>. That piece of code uses
Linq to Objects to project the results of the database query into a collection of SelectListItem objects. It's a cleaner way of doing this:
IEnumerable<SelectListItem> items = new IEnumerable<SelectListItem>();
foreach(var item on data){
items.Add(new SelectListItem {
Value = item.CategoryId.ToString(),
Text = item.CategoryName
});
}
Compiler Error Message: CS1061: 'WebMatrix.Data.Database' does not contain a definition for 'Select' and no extension method 'Select' accepting a first argument of type 'WebMatrix.Data.Database' could be found (are you missing a using directive
or an assembly reference?)
@{
Layout = "~/Shared/Layouts/_Layout.cshtml";
var productID = !UrlData[0].IsEmpty() ? UrlData[0] : "0";
var db = Database.Open("TechieTogsData");
var sql = "SELECT * FROM Products WHERE productID = @0";
var product = db.QuerySingle(sql, productID);
var items = db.Select(i => new SelectListItem { <------------ error is here... !!
Value = i.productId.ToString(),
Text = i.Title
});
}
pares101
Member
37 Points
37 Posts
Html.DropDownList Help ! How to link with database?? Razor syntax !
Feb 08, 2012 12:16 AM|LINK
How do i make a dropdown list with a database
I have a Products table
I want to show all the Products (productName) in tha drop down ... how is this possible????
Html.DropDownList
Rab Nawaz Kh...
Participant
967 Points
210 Posts
Re: Html.DropDownList Help ! How to link with database?? Razor syntax !
Feb 08, 2012 12:40 AM|LINK
Html.DropDownList
pares101
Member
37 Points
37 Posts
Re: Html.DropDownList Help ! How to link with database?? Razor syntax !
Feb 08, 2012 12:49 AM|LINK
Ok so... I have a
Table called products and inside products I have
productId
productName
productPrice
Html.DropDownList
Rab Nawaz Kh...
Participant
967 Points
210 Posts
Re: Html.DropDownList Help ! How to link with database?? Razor syntax !
Feb 08, 2012 12:53 AM|LINK
above code is ASP.NET, which technology you are using for your code ? simple HTML also requires some back end languages
pares101
Member
37 Points
37 Posts
Re: Html.DropDownList Help ! How to link with database?? Razor syntax !
Feb 08, 2012 12:54 AM|LINK
I am using razor :)
Mikesdotnett...
All-Star
155645 Points
19985 Posts
Moderator
MVP
Re: Html.DropDownList Help ! How to link with database?? Razor syntax !
Feb 08, 2012 04:40 AM|LINK
You can so this in one of two ways. The first uses plain HTML:
var sql = "SELECT ProductId, ProductName FROM Products"; var data = Database.Open("Northwind").Query(sql); <select name="productid"> @foreach(var row in data){ <option value="@row.ProductId">@row.ProductName</option> } </select>Or you can use the Html.DropDownList:
var sql = "SELECT ProductId, ProductName FROM Products"; var data = Database.Open("Northwind").Query(sql); var items = data.Select(i => new SelectListItem { Value = i.ProductId.ToString(), Text = i.ProductName }); @Html.DropDownList("productid", items)Web Pages CMS | My Site | Twitter
pares101
Member
37 Points
37 Posts
Re: Html.DropDownList Help ! How to link with database?? Razor syntax !
Feb 08, 2012 05:01 AM|LINK
Thank you Mikesdotnetting :)
Can you explain this of your code?
var items = data.Select(i => new SelectListItem { Value = i.ProductId.ToString(), Text = i.ProductNameMikesdotnett...
All-Star
155645 Points
19985 Posts
Moderator
MVP
Re: Html.DropDownList Help ! How to link with database?? Razor syntax !
Feb 08, 2012 06:24 AM|LINK
The Html.DropDownList helper expects an IEnumerable<SelectListItem>. That piece of code uses Linq to Objects to project the results of the database query into a collection of SelectListItem objects. It's a cleaner way of doing this:
IEnumerable<SelectListItem> items = new IEnumerable<SelectListItem>(); foreach(var item on data){ items.Add(new SelectListItem { Value = item.CategoryId.ToString(), Text = item.CategoryName }); }Web Pages CMS | My Site | Twitter
pares101
Member
37 Points
37 Posts
Re: Html.DropDownList Help ! How to link with database?? Razor syntax !
Feb 08, 2012 04:36 PM|LINK
Ok here is another follow up... I already have this on my page
@{
Layout = "~/Shared/Layouts/_Layout.cshtml";
var productID = !UrlData[0].IsEmpty() ? UrlData[0] : "0";
var db = Database.Open("ClothesData");
var sqlSelect = "SELECT * FROM Products WHERE productID = @0";
var product = db.QuerySingle(sqlSelect, productID);
}
How would I modify the code to make your code work???
pares101
Member
37 Points
37 Posts
Re: Html.DropDownList Help ! How to link with database?? Razor syntax !
Feb 08, 2012 05:20 PM|LINK
I keep on getting this error
Compiler Error Message: CS1061: 'WebMatrix.Data.Database' does not contain a definition for 'Select' and no extension method 'Select' accepting a first argument of type 'WebMatrix.Data.Database' could be found (are you missing a using directive or an assembly reference?)
@{
Layout = "~/Shared/Layouts/_Layout.cshtml";
var productID = !UrlData[0].IsEmpty() ? UrlData[0] : "0";
var db = Database.Open("TechieTogsData");
var sql = "SELECT * FROM Products WHERE productID = @0";
var product = db.QuerySingle(sql, productID);
var items = db.Select(i => new SelectListItem { <------------ error is here... !!
Value = i.productId.ToString(),
Text = i.Title
});
}