HTML.DropDownList values โ€‹โ€‹from multiple sources?

In ASP.NET MVC, is it possible to populate a list of Html.DropDownList values โ€‹โ€‹from multiple data sources along with multiple manually entered values?

Basically, I assume that it is formed as shown below, using something along the lines of 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 

I thought about using the idea of โ€‹โ€‹the database and getting data from it, however, I really do not really understand how to lay it out, as described above, using helpers and transfer data from it from several sources.

Thanks for any help in advance.

+4
source share
3 answers

As always, start with the model (actually start with the unit test, but not for this here):

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

Then the controller:

 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); } } 

And finally, a strongly typed model representation:

 <%@ 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> 

You will probably define a mediation type to avoid the anonymous types that I used for brevity.

Note. If your original question was about using OPTGROUP , then ignore my answer and make your intention clear so that you can get a more tailored answer.

0
source

It seems to be easier for you to write your assistant. The basic syntax for this is:

 // 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 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 (ie 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
source

There are many solutions for you. One of them will be described by Thomas, the other is a controller action that returns a PartialView, which contains code for visualizing input tags and options, another solution would be to force the Controller action to populate the ViewData with SelectList or select SelectList as the strong type for your View / ViewUserControl (Partial).

0
source

All Articles