Asp.net HTML.DropDownList值是否来自多个源?
在ASP.NET MVC中,是否可以将来自多个数据源的Html.DropDownList的值列表与多个手动输入的值一起填充 基本上,我设想它的格式如下所示,使用OPTGROUP的一些内容:Asp.net HTML.DropDownList值是否来自多个源?,asp.net,asp.net-mvc,vb.net,Asp.net,Asp.net Mvc,Vb.net,在ASP.NET MVC中,是否可以将来自多个数据源的Html.DropDownList的值列表与多个手动输入的值一起填充 基本上,我设想它的格式如下所示,使用OPTGROUP的一些内容: **Group 1** Manual Item 1 Manual Item 2 **Group 2** DS1 Item 1 DS1 Item 2 **Group 3** DS2 Item 1 DS2 Item 2 我曾经考虑过使用DB上的视图并从中获取数据,但是,我还不知道如何像上面那样使用帮助器进行
**Group 1**
Manual Item 1
Manual Item 2
**Group 2**
DS1 Item 1
DS1 Item 2
**Group 3**
DS2 Item 1
DS2 Item 2
我曾经考虑过使用DB上的视图并从中获取数据,但是,我还不知道如何像上面那样使用帮助器进行布局,以及如何从多个来源将数据传递给它
感谢您提前提供的帮助。您似乎更容易编写自己的帮助程序。执行此操作的基本语法如下:
// The class can be named anything, but must be static and accessible
public static class HtmlHelperExtensions
{
// The method name is what you want to call on Html,
// in this case Html.CoolSelectList(arguments...)
//
// The method has to be static, and the first argument should be of the type
// you're extending (in this case HtmlHelper, which is the type of the
// Html property on your view). The first argument must be prefixed with the
// "this" keyword, to indicate it's an extension method.
//
// All the following arguments will be arguments that you supply when calling
public static string CoolSelectList(this HtmlHelper helper,
IEnumerable<IEnumerable<CoolThingThatWeMakeAListOf>> groups)
{
// I chose an argument of type IEnumerable<IEnumerable<T>>, since that
// allows you to create each group of item on its own (i.e. get them from
// various data sources) and then add all of them to a list of groups
// that you supply as argument. It is then easy to keep track of which
// items belong to which groups, etc.
// Returned from the extension method is a string you have built, that
// constitutes the html you want to output on your view. I usually use
// the TagBuilder class to build the html.
return "this is what will be on the page";
}
}
//该类可以命名为任何名称,但必须是静态的和可访问的
公共静态类HtmlHelperExtensions
{
//方法名是要在Html上调用的,
//在本例中为Html.CoolSelectList(参数…)
//
//该方法必须是静态的,并且第一个参数的类型应为
//您正在扩展(在本例中是HtmlHelper,它是
//属性)。第一个参数必须以
//“this”关键字,表示它是一个扩展方法。
//
//以下所有参数都是调用时提供的参数
公共静态字符串CoolSelectList(此HtmlHelper帮助程序,
(可数组)
{
//我选择了IEnumerable类型的参数,因为
//允许您自己创建每组项目(即从
//各种数据源),然后将所有数据源添加到组列表中
//你作为论据提供的。然后很容易跟踪哪个
//项目属于哪些组等。
//从扩展方法返回的是您构建的字符串
//构成要在视图上输出的html。我通常使用
//用于构建html的TagBuilder类。
return“这是将出现在页面上的内容”;
}
}
有许多解决方案可以解决您的问题。一个是Tomas描述的,另一个是返回PartialView的控制器操作,其中包含呈现输入和选项标记的代码,另一个解决方案是让控制器操作使用SelectList填充ViewData,或者让SelectList作为View/ViewUserControl(部分)的强类型.通常从模型开始(实际上从单元测试开始,但这里没有时间):
最后是模型的强类型视图:
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MyApp.Models.MyModel>" %>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<%= Html.DropDownListFor(x => x.SelectedItem, Model.Items) %>
</asp:Content>
然后忽略我的答案,明确你的意图,这样你就能得到一个更合适的答案
public class HomeController : Controller
{
public ActionResult Index()
{
var items1 = new[]
{
new { Value = "1", Text = "Manual Item 1" },
new { Value = "2", Text = "Manual Item 2" },
};
// TODO: Go fetch those from your repo1
var items2 = new[]
{
new { Value = "3", Text = "DS1 Item 1" },
new { Value = "4", Text = "DS1 Item 2" },
};
// TODO: Go fetch those from your repo2
var items3 = new[]
{
new { Value = "5", Text = "DS2 Item 1" },
new { Value = "6", Text = "DS2 Item 2" },
};
var items = items1.Concat(items2).Concat(items3);
var model = new MyModel
{
Items = new SelectList(items, "Value", "Text")
};
return View(model);
}
}
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MyApp.Models.MyModel>" %>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<%= Html.DropDownListFor(x => x.SelectedItem, Model.Items) %>
</asp:Content>