简体   繁体   English

如何实现从多个sql表读取的级联下拉列表? C#MVC

[英]How would I implement a cascading dropdown list that reads from multiple sql tables? C# MVC

I'm building an mvc web app using C#. 我正在使用C#构建MVC Web应用程序。 The place I am currently stuck is trying to add a cascading drop-down list. 我当前被卡住的地方正在尝试添加级联下拉列表。 I have seen several articles on this, however my usage varies enough that I'm not sure how to implement it. 我已经看过几篇关于此的文章,但是我的用法相差很大,因此我不确定如何实现它。

My information to populate the dropdown lists is coming from a series of sql tables. 我填充下拉列表的信息来自一系列sql表。 The first dropdown should show the information from one table (which lists available tables). 第一个下拉列表应显示一个表(列出可用表)中的信息。 When the user selects an option, a second dropdown list should be created and filled with the information from the chosen table. 当用户选择一个选项时,应创建第二个下拉列表,并用所选表中的信息填充。

How would I implement this cascading dropdown list which could read from any number of different tables? 我将如何实现这个可以从任意数量的不同表中读取的级联下拉列表?

I believe this will involve AJAX/javascript which I'm not very familiar with. 我相信这会涉及到我不太熟悉的AJAX / javascript。

To better explain my data structure, this is an application for an administrator to keep track of useful information. 为了更好地说明我的数据结构,这是管理员可用来跟踪有用信息的应用程序。 There could be several tables such as Employees, CompaniesWorkedWith, ProjectsBeingDeveloped, BudgetItems, etc... Then a final lookup table that would contain these table names. 可能有几个表,例如Employees,CompanyWorkedWith,ProjectsBeingDeveloped,BudgetItems等。然后是一个包含这些表名的最终查找表。 The first dropdown would be populated with information from this lookup table. 第一个下拉列表将使用此查找表中的信息填充。 Then when the user selects an item (such as Employee) the second dropdown would grab the information from the appropriate table and display it in a dropdown. 然后,当用户选择一个项目(例如Employee)时,第二个下拉列表将从相应的表中获取信息,并将其显示在下拉列表中。 Then the user could select and item (such as an individual employee) which will then grab the details of that item and present it to the user. 然后,用户可以选择和选择项目(例如单个员工),然后将获取该项目的详细信息并将其显示给用户。 That last part should be easy. 最后一部分应该很容易。 I'm just confused on how to get the dropdowns to cascade through multiple tables. 我只是对如何使下拉列表级联多个表感到困惑。 It should be dynamic that someone can easily go in and add another table to the lookup table and have it function properly. 有人可以轻松进入并将另一个表添加到查找表并使它正常运行应该是动态的。

You need to implement callback's to update the data sources for the cascaded controls. 您需要实现回调来更新级联控件的数据源。 Just use your update panels appropriately to prevent full page post-backs. 只需适当使用更新面板即可防止回发整页。 If you're using a framework such as DevExpress, Telerik or ASP.NET then there's lots of options and sample source code for this exact thing. 如果您使用的是诸如DevExpress,Telerik或ASP.NET之类的框架,那么对于这个确切的东西,有很多选项和示例源代码。

http://www.codeproject.com/Articles/730953/Cascading-Dropdown-List-With-MVC-LINQ-to-SQL-and-A http://www.codeproject.com/Articles/730953/Cascading-Dropdown-List-With-MVC-LINQ-to-SQL-and-A

EDIT: I realized you're second data-source depends on a second more dynamic data source. 编辑:我意识到您是第二个数据源取决于第二个更动态的数据源。 To do this I would create my own data-source implmenting IEnumerable. 为此,我将创建自己的实现IEnumerable的数据源。 I have a sample of something that may help somewhere... I'll look for it. 我有一些可能对某些地方有所帮助的示例...我会寻找的。

EDIT 2: Okay, I took a look at code similar to this (not quite the same) but here's how you can do it: 编辑2:好的,我看了一下与此类似的代码(不太一样),但是这是您可以做到的:

  1. You'll need a generic container for your values for your second/third/etc. 您需要一个通用容器来存储第二个/第三个/等的值。 dropdown lists. 下拉列表。 This might be a List<string> or a List<MyGenericClass> with whatever properties you are filling out to represent your columns for each row in the dropdown. 这可能是List<string>List<MyGenericClass> ,具有要填充的任何属性以表示下拉列表中每一行的列。

  2. Use SqlConnection , SqlCommand to iterate through the results and populate the List (or your own class that implements IEnumerable). 使用SqlConnectionSqlCommand遍历结果并填充List (或您自己的实现IEnumerable的类)。

  3. Assign the IEnumerable object to your dropdown list datasource. 将IEnumerable对象分配给您的下拉列表数据源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM