ListBox
Complex Type
Features
Sample
Description
This sample shows how to bind to a list of complex type using "DisplayMemberPath" and "SelectedValuePath".
Source
ComplexTypeController.cs
using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Web; using System.Web.Mvc; using MvcExplorer.Models; namespace MvcExplorer.Controllers { public partial class ListBoxController : Controller { public ActionResult ComplexType() { var list = GetSystemColors(); return View(list); } private static NamedColor[] GetSystemColors() { return Enum.GetValues(typeof(KnownColor)) .Cast<KnownColor>() .Select(c => new NamedColor { Name = c.ToString(), Value = "#" + Color.FromKnownColor(c).ToArgb().ToString("X8").Substring(2) }) .ToArray(); } } }
ComplexType.cshtml
@model IEnumerable<MvcExplorer.Models.NamedColor> <div> <label>@Html.Raw(Resources.ListBox.ComplexType_SelectAnItem)</label> @(Html.C1().ListBox() .Bind(Model) .DisplayMemberPath("Name") .SelectedValuePath("Value") .Width(250).Height(200) ) </div> @section Description{ @Html.Raw(Resources.ListBox.ComplexType_Text0) }
Documentation