views:

361

answers:

2

i just could not figure out how i will put my data on a selectlist for it to be displayed on a dropdownlist..

i am using mysql as my database.. on my model i have this query:

Imports Microsoft.VisualBasic
Imports System.Data

Public Class ClassCategoryConnection
Inherits ClassConnection

    Public Function SelectCategory() As DataTable
         Return ReadData("SELECT IDcategory, category FROM category")
     End Function
End Class

on my controller i have:

Public Class HomeController
Private Category As New ClassCategoryConnection
    Function Index() As ActionResult
        Dim _category As DataTable = Category.SelectCategory()
        Return View(_category)
    End Function
End Class

how will i construct my selectlist with this?..=)

thank you in advance!

+3  A: 

You can do it in controller part and send the select list to view.

[Controller]

public IEnumerable<SelectListItem> List
{
     get
     {
          List<SelectListItem> list = new List<SelectListItem>();
          foreach(var data in _category){
                list.Add(new SelectListItem
                {
                     Text = data.field,
                     Value = data.field,
                 });
           }
          return list;
     }    
}

In the Index Action add the following code

ViewData["dropdownlist_name"] = List

In the View just create

<%=Html.DropDownList("dropdownlist_name") %>
santose
can you please convert it to vb.net?i can't seem to convert it on the link provided by lucky below..thank you!
tiff
thank you! it worked=)
tiff
thanks a lot!!!!
goths
A: 

In C# ,

Please convert it to to VB for your liking Here...

first thing passing a data table is a headache try passing it through the view data like

  return View (Category.SelectCategory().AsEnumerable().ToList());

and in the aspx page try using that like this

    Html.DropDownList("list",viewdata.model)

this should work ...

lucky
i got an error on the aspx page that says: Overload resolution failed because no accessible 'DropDownList' can be called without a narrowing conversion but when i removed the "list" it gave me this error: Conversion from type 'List(Of DataRow)' to type 'String' is not valid.
tiff