– dropdownlist in mvc

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!

Best Solution

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


public IEnumerable<SelectListItem> List
          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") %>