ASP.NET MVC Input 101

This page shows how to get started with MVC's Input controls.

Getting Started

Steps for getting started with Input controls in ASP.NET MVC applications:

  1. Create a new MVC project using the C1 ASP.NET MVC application template.
  2. Add controller and corresponding view to the project.
  3. Initialize the input control in view using razor syntax.
  4. (Optional) Add some CSS to customize the input control's appearance.
<!DOCTYPE html> <html> <body> @(Html.C1().InputNumber().Value(3.5).Step(.5).Format("n")) </body> </html>
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using Input101.Models; namespace Input101.Controllers { public class HomeController : Controller { public ActionResult Index() { return View(); } } }

Result (live):

AutoComplete

The AutoComplete control is an auto-complete control that allows you to filter its item list as you type, as well as select a value directly from its drop-down list.

To use the AutoComplete control, you must minimally set the Bind property to an array of data in order to populate its item list. The AutoComplete control also offers several other properties to alter its behavior, such as the CssMatch property. The CssMatch property allows you to specify the CSS class that is used to highlight parts of the content that match your search terms.

The example below uses List of strings to populate the AutoComplete control's item list using the Bind property. To see a list of suggestions, type "ab" or "za" in the AutoComplete controls below.

<div> <label>Bind Only</label> @(Html.C1().AutoComplete().Bind(Model.CountryList)) </div> <div> <label>Bind &amp; CssMatch</label> @(Html.C1().AutoComplete().Bind(Model.CountryList).CssMatch("highlight")) </div>
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using Input101.Models; namespace Input101.Controllers { public class HomeController : Controller { public ActionResult Index() { return View(new InputModel()); } } }
.highlight { background-color: #ff0; color: #000; }

Result (live):

ComboBox

The ComboBox control is very similar to the AutoComplete control, but rather than providing a list of suggestions as you type, the ComboBox will automatically complete and select the entry as you type.

Like the AutoComplete control, you must minimally set the ComboBox's Bind property to an array of data in order to populate its item list. You may also want to specify whether the ComboBox is editable via the IsEditable property. The IsEditable property determines whether or not a user can enter values that do not appear in the ComboBox's item list.

The example below uses two ComboBoxes bound to the same data source as the AutoComplete control above. The first ComboBox's isEditable property is set to false, while the second ComboBox's IsEditable property is set to true.

<div> <label>Non-Editable</label> @(Html.C1().ComboBox().Bind(Model.CountryList).IsEditable(false)) </div> <div> <label>Editable</label> @(Html.C1().ComboBox().Bind(Model.CountryList).IsEditable(true)) </div>
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using Input101.Models; namespace Input101.Controllers { public class HomeController : Controller { public ActionResult Index() { return View(new InputModel()); } } }

Result (live):

InputDate & Calendar

The InputDate control allows you to edit and select dates via a drop-down calendar, preventing you from entering an incorrect value. The InputDate's drop-down calendar was developed as a separate control and can be used be used independently from the InputDate control.

Both InputDate and Calendar, specify several properties to alter the controls' behavior. The most commonly used properties include:

The example below demonstrates how to use each of these properties.

@{ var today = DateTime.Today.Date; var minDate = new DateTime(today.Year, 1, 1); var maxDate = new DateTime(today.Year, 12, 31); } <div> <label>Bound InputDate with min &amp; max</label> @(Html.C1().InputDate().Id("idcInputDate").Value(today).Min(minDate).Max(maxDate)) </div> <div> <label>Bound Calendar with min &amp; max</label> @(Html.C1().Calendar().Id("idcCalendar").Value(today).Min(minDate).Max(maxDate).Width("300px")) </div> <p> <b>Valid Range: <span id="idcMinDate">@minDate</span> to <span id="idcMaxDate"></span>@maxDate</b> </p>
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using Input101.Models; namespace Input101.Controllers { public class HomeController : Controller { public ActionResult Index() { return View(); } } }

Result (live):

Valid Range: 1/1/2024 12:00:00 AM to 12/31/2024 12:00:00 AM

InputDate, InputTime and InputDateTime

Similar to the InputDate control, the InputTime control allows you to modify the time portion of a JavaScript date. The InputTime control shares many of the same properties as the InputDate control, including Format, Min, Max, and Value. The InputTime control also offers a Step property that allows you to specify the number of minutes between entries in its drop-down list.

The InputDateTime control combines the InputDate and InputTime controls, allowing you to set the date and time portions. The InputDateTime control has two drop-downs: a Calendar for picking dates, and a list for picking times.

The example below illustrates how to use the InputTime control in conjunction with the InputDate control. Notice that these controls work together to edit the same DateTime JavaScript Object and only update the part of the DateTime that they are responsible for.

The example also shows an InputDateTime that updates both the date and time parts.

@{ var today = DateTime.Today.Date; var minDate = new DateTime(today.Year, 1, 1); var maxDate = new DateTime(today.Year, 12, 31); var minTime = new DateTime(DateTime.Today.Year, DateTime.Today.Month, DateTime.Today.Day, 7, 0, 0); var maxTime = new DateTime(DateTime.Today.Year, DateTime.Today.Month, DateTime.Today.Day, 19, 0, 0); var format = "MMM dd, yyyy"; } <div> <label>Bound InputDate with Min, Max, &amp; Format</label> @(Html.C1().InputDate().Id("iditInputDate").Value(today).Min(minDate).Max(maxDate).Format(format).OnClientValueChanged("inputDate_ValueChanged")) </div> <div> <label>Bound InputTime with Min, Max, &amp; Step</label> @(Html.C1().InputTime().Id("iditInputTime").Value(today).Min(minTime).Max(maxTime).Step(15).OnClientValueChanged("inputTime_ValueChanged")) </div> <div> <label>Bound InputDateTime with Min, Max, TimeMin, TimeMax, Format, &amp; TimeStep</label> @(Html.C1().InputDateTime().Id("iditInputDateTime").Value(today).Min(minDate).Max(maxDate).TimeMin(minTime).TimeMax(maxTime).TimeStep(15).OnClientValueChanged("inputDateTime_ValueChanged")) </div> <p> <b>Selected Date & Time: <span id="iditSelectedValue"></span></b> </p>
function inputTime_ValueChanged(sender) { var inputDateTime = wijmo.Control.getControl("#iditInputDateTime"); inputDateTime.value = wijmo.DateTime.fromDateTime(inputDateTime.value, sender.value); } function inputDate_ValueChanged(sender) { var inputDateTime = wijmo.Control.getControl("#iditInputDateTime"); inputDateTime.value = wijmo.DateTime.fromDateTime(sender.value, inputDateTime.value); } function inputDateTime_ValueChanged(sender) { var inputDate = wijmo.Control.getControl("#iditInputDate"); var inputTime = wijmo.Control.getControl("#iditInputTime"); inputDate.value = wijmo.DateTime.fromDateTime(sender.value, inputDate.value); inputTime.value = wijmo.DateTime.fromDateTime(inputTime.value, sender.value); document.getElementById('iditSelectedValue').innerHTML = wijmo.Globalize.format(sender.value, 'MMM dd, yyyy h:mm:ss tt'); }
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using Input101.Models; namespace Input101.Controllers { public class HomeController : Controller { public ActionResult Index() { return View(); } } }

Result (live):

Selected Date & Time:

ListBox

The ListBox control displays a list of items and allows you to select items using your mouse and keyboard. Like the AutoComplete and ComboBox controls, you must specify the ListBox's Bind property in order to use the control.

The example below allows you to select an item within the ListBox control, and also displays the control's SelectedIndex and SelectedValue properties.

<div> @(Html.C1().ListBox().Id("lbListBox").Height("150px").Width("250px").Bind(Model.CitiesList).OnClientSelectedIndexChanged("selectedIndexChanged")) </div> <p> <b>selectedIndex: <span id="lbSelIdx"></span></b> </p> <p> <b>selectedValue: <span id="lbSelVal"></span></b> </p>
$(document).ready(function () { var ListBox = wijmo.Control.getControl("#lbListBox"); selectedIndexChanged(ListBox); }); //selectedIndexChanged event handler function selectedIndexChanged(sender) { //set selectedIndex and selectedValue text if (document.getElementById("lbListBox") && document.getElementById("lbSelIdx") && document.getElementById("lbSelVal")) { document.getElementById('lbSelIdx').innerHTML = sender.selectedIndex; document.getElementById('lbSelVal').innerHTML = sender.selectedValue; } }
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using Input101.Models; namespace Input101.Controllers { public class HomeController : Controller { public ActionResult Index() { return View(); } } }

Result (live):

selectedIndex:

selectedValue:

InputNumber

The InputNumber control allows you to edit numbers, preventing you from entering invalid data and optionally formatting the numeric value as it is edited. The InputNumber can be used without specifying any of its properties; however, you'll typically want to bind it to some data using the Value property.

In addition to the value property, the InputNumber control offers several other properties that can be used to alter its behavior, such as:

The example below demonstrates how to use all of these properties.

<div> <label>Unbound with "n0" format</label> @(Html.C1().InputNumber().Id("inInputNumber1").Value(0).Format("n0")) </div> <div> <label>Bound with "n" format</label> @(Html.C1().InputNumber().Id("inInputNumber2").Value(Math.PI).Format("n")) </div> <div> <label>Bound with min (0), max (10), step, and "c2" format</label> @(Html.C1().InputNumber().Id("inInputNumber3").Value(3.5).Format("C2").Step(.5).Min(0).Max(10)) </div> <div> <label>Unbound with placeholder and isRequired="false"</label> @(Html.C1().InputNumber().Id("inInputNumber4").Placeholder("Enter a number...").IsRequired(false).Value(null)) </div>
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using Input101.Models; namespace Input101.Controllers { public class HomeController : Controller { public ActionResult Index() { return View(); } } }

Result (live):

InputMask

The InputMask control allows you to validate and format user input as it is entered, preventing invalid data. The InputMask control can be used without specifying any of its properties; however, you will typically set its Value and Mask properties. Like the other MVC input controls, the Value property specifies the value for the InputMask control. The Mask property specifies the control's mask and supports a combination of the following characters:

0
Digit.
9
Digit or space.
#
Digit, sign, or space.
L
Letter.
l
Letter or space.
A
Alphanumeric.
a
Alphanumeric or space.
.
Localized decimal point.
,
Localized thousand separator.
:
Localized time separator.
/
Localized date separator.
$
Localized currency symbol.
<
Converts characters that follow to lowercase.
>
Converts characters that follow to uppercase.
|
Disables case conversion.
\
Escapes any character, turning it into a literal.
All others
Literals.

The examples below demonstrates how to use the Value and Mask properties with the InputMask, InputDate, and InputTime controls.

<div> <label>Social Security Number</label> @(Html.C1().InputMask().Id("imSocial").Mask("000-00-0000").HtmlAttributes(new { title = "Mask: 000-00-0000" })) </div> <div> <label>Phone Number</label> @(Html.C1().InputMask().Id("imPhone").Mask("(999) 000-0000").HtmlAttributes(new { title = "Mask: (999) 000-0000" })) </div> <div> <label>Try your own</label> @(Html.C1().InputMask().Id("imCustomInput") .Placeholder("Enter an input mask...") .OnClientValueChanged("MaskvalueChanged") ) @(Html.C1().InputMask().Id("imCustomTrial") .Placeholder("Try your input mask...") ) </div> <div> <label>InputDate with Mask</label> @(Html.C1().InputDate().Value(DateTime.Now) .Format("MM/dd/yyyy").Mask("99/99/9999") .HtmlAttributes(new { title = "Mask: 99/99/9999" }) ) </div> <div> <label>InputTime with Mask</label> @(Html.C1().InputTime().Value(DateTime.Now) .Format("hh:mm tt").Mask("00:00 >LL") .HtmlAttributes(new { title = "Mask: 00:00 >LL" }) ) </div>
// InputMask valueChanged event handler function MaskvalueChanged(sender) { var customMaskTrial = wijmo.Control.getControl("#imCustomTrial"); customMaskTrial.mask = sender.value; customMaskTrial.hostElement.title = 'Mask: ' + (sender.value || 'N/A'); }
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using Input101.Models; namespace Input101.Controllers { public class HomeController : Controller { public ActionResult Index() { return View(); } } }

Result (live):

Menu

The Menu control allows you to create a simple drop-down list with clickable items. The Menu's items can be defined directly or by using the Bind property similar to the ComboBox. To specify the text displayed on the Menu, you can set the Header property.

The Menu control offers two ways to handle user selections, specifying a command on each menu item and the ItemClicked event. Unlike the ItemClicked event, commands are objects that implement two methods:

The example below demonstrates how to use both approaches.

<div> <label>itemClicked Event</label> @(Html.C1().Menu().Id("mFileMenu").Header("File").OnClientItemClicked("itemClicked") .MenuItems(items => items .Add(item => item.Header("New: create a new document")) .Add(item => item.Header("Open: load an existing document from a file")) .Add(item => item.Header("Save: save the current document to a file")) .Add(item => item.IsSeparator(true)) .Add(item => item.Header("Exit: save changes and exit the application")) )) @(Html.C1().Menu().Id("mEditMenu").Header("Edit") .OnClientItemClicked("itemClicked") .MenuItems(items => { items.Add().Header("Cut: move the current selection to the clipboard"); items.Add().Header("Copy: copy the current selection to the clipboard"); items.Add().Header("Paste: insert the clipboard content at the cursor position"); items.Add().IsSeparator(true); items.Add().Header("Find: search the current document for some text"); items.Add().Header("Replace: replace occurrences of a string in the current document"); }) ) </div> <div class="app-input-group"> <label>Commands</label> @(Html.C1().Menu().Header("Change Tax") .Command(cmd => cmd.ExecuteCommand("execute").CanExecuteCommand("canExecute")) .MenuItems(items => { items.Add().Header("+ 25%").CommandParameter(0.25); items.Add().Header("+ 10%").CommandParameter(0.10); items.Add().Header("+ 5%").CommandParameter(0.05); items.Add().Header("+ 1%").CommandParameter(0.01); items.Add().IsSeparator(true); items.Add().Header("- 1%").CommandParameter(-0.01); items.Add().Header("- 5%").CommandParameter(-0.05); items.Add().Header("- 10%").CommandParameter(-0.10); items.Add().Header("- 25%").CommandParameter(-0.25); }) ) @(Html.C1().InputNumber().Id("mInputNumber") .Value(0.07).Step(0.05).Format("p0").Min(0).Max(1) ) </div>
function itemClicked(sender) { alert('You\'ve selected option ' + sender.selectedIndex + ' from the ' + sender.header + ' menu!'); } function execute(arg) { var inputNumber = wijmo.Control.getControl("#mInputNumber"); // convert argument to Number arg = wijmo.changeType(arg, wijmo.DataType.Number); // check if the conversion was successful if (wijmo.isNumber(arg)) { // update the value inputNumber.value += arg; } } function canExecute(arg) { var inputNumber = wijmo.Control.getControl("#mInputNumber"); // convert argument to Number arg = wijmo.changeType(arg, wijmo.DataType.Number); // check if the conversion was successful if (wijmo.isNumber(arg)) { var val = inputNumber.value + arg; // check if the value is valid return val >= 0 && val <= 1; } return false; }
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using Input101.Models; namespace Input101.Controllers { public class HomeController : Controller { public ActionResult Index() { return View(); } } }

Result (live):