Dropdown list in asp.net mvc

I just couldn't figure out how I put my data in the select list so that it appears in the dropdown.

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:

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 do i build my favorites list with this? .. =)

Thank you in advance!

+2


source to share


2 answers


You can do this in the controller part and submit the select list for viewing.

[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 activity add the following code



ViewData["dropdownlist_name"] = List

      

In the view, just create

<%=Html.DropDownList("dropdownlist_name") %>

      

+3


source


In C #,

Convert it to VB as you see fit Here ...

First thing passing data table is headache trying to pass it through data like

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

      



and in the aspx page try using this like

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

      

this should work ...

0


source







All Articles