Example of ColorPickerFor in ASP.NET MVC ColorPicker Control
Click the submit button to post the current value of the ColorPicker. When submitting with color value `#000080`, validation error message will be shown by using Data Annotation.
Choose a color
@Html.UiColorPickerFor(model => model.colorValue, new UiColorPickerOptions())
@Html.UiButton(new UiButtonOptions { Id = "submit-button", Text = "Submit", Type = UiButtonType.Submit, Color = UiButtonColor.Primary })
<style>
#default-control, #inline-control, #custom-control, #colorpicker-control { text-align: center; padding: 20px 0; }
#default-control h4, #colorpicker-control h4, #inline-control h4 { margin: 0 0 12px; font-size: 14px; font-weight: 600; }
.color-preview { width: 120px; height: 80px; margin: 0 auto 16px; border: 1px solid var(--ui-border); border-radius: 8px; background: #008000; }
.color-row { display: flex; flex-wrap: wrap; justify-content: center; gap: 28px; }
.property-section .property-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 6px 0; }
.property-section .prop-input { width: 110px; padding: 4px 6px; border: 1px solid var(--ui-input-border); border-radius: var(--ui-radius); background: var(--ui-surface); color: var(--ui-text); font: inherit; }
#default-control .field-validation-error { display: block; color: red; margin: 8px 0; }
#default-control form { display: inline-block; text-align: center; }
#submit-button { margin-top: 16px; }
</style>
@using Lumen.UI.Mvc.Demo.Controllers
@model InputsDemoController.ColorPickerModel
<div class="control-section">
<div id="default-control">
<h4>Choose a color</h4>
@using (Html.BeginForm())
{
@Html.UiColorPickerFor(model => model.colorValue, new UiColorPickerOptions())
@Html.ValidationMessageFor(model => model.colorValue)
@Html.UiButton(new UiButtonOptions { Id = "submit-button", Text = "Submit", Type = UiButtonType.Submit, Color = UiButtonColor.Primary })
}
</div>
</div>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
namespace YourApp.Controllers
{
public class ColorPickerController : Controller
{
// The page is Views/ColorPicker/ColorPickerFor.cshtml; it draws what ViewData holds.
public ActionResult ColorPickerFor()
{
ColorPickerModel model = new ColorPickerModel();
model.colorValue = "#000080";
return View(model);
}
[HttpPost]
public ActionResult ColorPickerFor(ColorPickerModel model)
{
// the posted value is obtained from the model
return View(model);
}
public class ColorPickerModel
{
[System.ComponentModel.DataAnnotations.RegularExpression("^((?!#000080).)*$", ErrorMessage = "Please select color with value other than #000080.")]
public string colorValue { get; set; }
}
}
}
@* Install once: Install-Package Lumen.UI.Mvc (or: dotnet add package Lumen.UI.Mvc) *@
@* Views/Web.config: <add namespace="Lumen.UI.Mvc" /> *@
@* Views/Shared/_Layout.cshtml *@
<head>
@Html.UiStyles()
<link rel="stylesheet" href="~/Content/site.css" />
</head>
<body>
@RenderBody()
@Html.UiScripts()
@RenderSection("scripts", required: false)
</body>
This sample demonstrates strongly typed support in ColorPicker. You can bind model property to the ColorPicker by using Lambda Expression. While submitting form, the model property will be based on current value of the ColorPicker.