Getting Started with the DateInput
This tutorial explains how to set up a basic Telerik UI for ASP.NET MVC DateInput and highlights the major steps in the configuration of the component.
You will initialize a DateInput component and set a default value.
Prerequisites
To successfully complete the tutorial, you need a project that is already configured to use the Telerik UI for ASP.NET MVC components:
To create a new pre-configured project for the Telerik UI for ASP.NET MVC components, you can use a project template.
To manually configure an existing project by using NuGet, see the Adding Telerik UI through NuGet.
1. Prepare the CSHTML File
The first step is to add the required directives at the top of the .cshtml
document:
-
To use the Telerik UI for ASP.NET MVC HtmlHelpers:
@using Kendo.Mvc.UI
2. Initialize the DateInput
Use the DateInput HtmlHelper to add the component to the page:
- The
Name()
configuration method is mandatory as its value is used for theid
and thename
attributes of the DateInput element. - The
Label()
configuration specifies the label text of the DateInput. Optionally, you can enable theFloating
feature of the label.
Do not set the
Name()
option when usingDateInputFor
. The[ComponentName]For
method automatically sets the control'sName()
to the field it is bound to. For more information, see the Fundamentals article.
@(Html.Kendo().DateInput()
.Name("dateinput")
.Label(label=>{
label.Content("DateInput:")
})
)
3. Set a Default Value
The next step is to set a default value. The following example shows how to use the .Value()
method of the DateInput, to set the current date as a value of the component.
@{
var defaultDateInput = DateTime.Now;
}
@(Html.Kendo().DateInput()
.Name("dateinput")
.Label(label=>{
label.Content("DateInput:")
})
.Value(defaultDateInput)
)
4. (Optional) Reference Existing DateInput Instances
To use the client-side API of the DateInput and build on top of its initial configuration, create a reference to the DateInput instance. Once you get a valid reference, you can call the respective API methods:
-
Use the
.Name()
(id
attribute) of the component instance to get a reference.<script> $(document).ready(function() { var dateinputReference = $("#dateinput").data("kendoDateInput"); // dateinputReference is a reference to the existing DateInput instance of the helper. }) </script>
-
Use the DateInput client-side API to control the behavior of the control. In this example, you will use the
enable
method to disable the input.<script> $(document).ready(function() { var dateinputReference = $("#dateinput").data("kendoDateInput"); // dateinputReference is a reference to the existing DateInput instance of the helper. dateinputReference.enable(false); // enable or disable the input }) </script>