来自多个数据源的HTML.DropDownList值?

4
在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

我考虑使用数据库视图并从中获取数据,但是,我不知道如何像上面那样使用帮助程序来布局,并从多个来源传递数据。

提前感谢您的任何帮助。

3个回答

0

看起来你自己编写帮助程序会更容易。实现这个的基本语法如下:

// 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";
    }
}

0

像往常一样,从一个模型开始(实际上应该从单元测试开始,但这里没有时间):

public class MyModel
{
    public string SelectedItem { get; set; }
    public IEnumerable<SelectListItem> Items { get; set; }
}

然后是控制器:

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>

你可能会定义一个中间类型来避免我为了简洁而使用的匿名类型。
备注:如果您最初的问题是关于使用 OPTGROUP,请忽略我的答案并明确您的意图,以便您可以获得更适合的答案。

0

您的问题有许多解决方案。其中一个是Tomas所描述的,另一个是返回PartialView的控制器操作,其中包含呈现输入和选项标记的代码,另一种解决方案是将Controller操作填充到ViewData中,或者将SelectList作为View/ViewUserControl(Partial)的强类型。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接