3

I need to show all the table names which are in my database into a dropdownlist.

When table is selected, I need to show another dropdownlist with columns. How can I do that?

I am using ASP.NET, MVC and EntityFramework.

Update : KnockoutJs: ASP.NET MVC 4 Dynamic Forms solved my dynamic dropdownlist problem.

Melih
  • 558
  • 1
  • 7
  • 33

1 Answers1

3

Here is a KnockoutJs approach: ASP.NET MVC Dynamic Forms

Getting tables using Entity Framework:

using ( var ctx = new ObjectContext() )
{
    List<string> results = ctx.ExecuteStoreQuery<string>("SELECT name FROM sys.tables ORDER BY name").ToList();
}

Then just bind your results to a DropdownList. Knockout documentation is pretty good. Here is how to bind values to select/option

Community
  • 1
  • 1
Andrei
  • 42,814
  • 35
  • 154
  • 218
  • ok. now I understand to add new dropdownlist. how can I call all database tables name into dropdownlist ? – Melih Jan 17 '14 at 14:49
  • 1
    The query above will return a `List` which can be bound to the dropdown list. – Ric Jan 17 '14 at 14:56
  • Whatever you do, do not put variables in that SQL string to make it *dynamic*. You are already giving away column names and table names for free. This has the beginnings of a SQL Injection nightmare. – Lotok Jan 17 '14 at 15:00
  • @Ric ok I need to get string query value from dropdown lists. – Melih Jan 17 '14 at 15:09
  • @James the users are administrators so they want to send query and want to see result of this query. So they should see table names and column names – Melih Jan 17 '14 at 15:10