多重动态填充下拉列表 [英] Multiple Dynamically Populated Drop Down Lists
问题描述
我有一个网页,其中将有大约十下拉这是从我的SQL数据库生成的列表。什么是填充它们的最好方法?我要使用存储过程使用LINQ返回多个结果集,但似乎有点大材小用。是否有这样做的另一种方式?使用HtmlHelpers或类似的东西?
I've got a page which will have about ten drop down lists which are generated from my SQL database. What's the best way to populate them? I was going to use a stored procedure with LINQ to return multiple result sets, but it seems a bit overkill. Is there another way of doing it? Using HtmlHelpers or something like that?
看着,仿佛每个人似乎都被这所迷惑,我会阐述。
Seeing as though everyone seems to be confused by this, I will elaborate.
- 这是不是与缓存的事,这不是什么问题状态
- 这不是做与ASP.NET控件,如DropDownList的,我标记它asp.net-MVC
- 这是不是与code-落后车型做的,我认为这是由我如何标记这个问题最初是作为ASP.NET MVC 隐明显
问题是所需的页面下拉列表上的多个结果集(HTML想!)。所以我有一个下拉列表中你最喜欢的獾品种,一个下拉列表中你有多少有生日,一个下拉列表今天有多少云在天空中。所有这些都是动态填充(请注意,我是在开玩笑,这是一个金融体系,我的工作)。我需要所有这些是我的视图页面上,但我宁可不使用IMultipleResult返回类型的LINQ存储过程带回多个结果集。它只是变得混乱。
The problem is that multiple results sets are required on the page for drop down lists (think HTML!). So I have a drop down list for your favourite breed of badger, a drop down list for how many birthdays you've had, a drop down list for how many clouds are in the sky today. All of these are dynamically populated (please note, I am joking, this is a finance system I work on). I need all of them to be on my view page, but I'd rather NOT use the IMultipleResult return type in a LINQ stored procedure to bring back multiple result sets. It just gets messy.
因此,在基本的,我想大约10下拉列表我的视图页面上,所有这些都填充了数据从数据库(它不断变化)。是什么让他们的观点的最佳方式?
So in basic, I want about 10 drop down lists on my view page, all of which are populated with data from a database (which constantly change). What is the best way to get them on the view?
推荐答案
我只想所需的数据传递给视图,无论是在多个ViewData的词典或作为一种特殊的视图模型,如果你想强类型的访问。然后使用HtmlHelper.DropDownList()来显示实际的下拉菜单。
I would just pass the required data to the view, either in multiple ViewData dictionaries or as a special view model if you want strongly typed access. Then use HtmlHelper.DropDownList() to display the actual drop downs.
ViewData["Data1"] = SomeRepository.GetList();
ViewData["Data2"] = SomeRepositoty.GetList();
return View();
查看
<%= Html.DropDownList("Data1") %>
<%= Html.DropDownList("Data2") %>
强类型的解决方案
查看模型
public class DataViewModel
{
public IEnumerable<string> Data1 { get; set; }
public IEnumerable<string> Data2 { get; set;}
}
控制器
var model = SomeRepository.GetModel(); // returns an instance of DataViewModel
return View(model);
查看
<%= Html.DropDownList("Data1", new SelectList(Model.Data1)) %>
<%= Html.DropDownList("Data2", new SelectList(Model.Data2)) %>
这篇关于多重动态填充下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!