# Getting Started with the NumericTextBox

This tutorial explains how to set up a basic Telerik UI for ASP.NET MVC NumericTextBox and highlights the major steps in the configuration of the component.

You will initialize two NumericTextBox components, one of them will have an event handler.

![Sample Telerik UI for ASP.NET MVC NumericTextBox](https://www.telerik.com/aspnet-mvc/documentation/assets/959fc0df558e72eadfef6c8d553119a9/numerictextbox-getting-started.png)

## 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](https://www.telerik.com/aspnet-mvc/documentation/getting-started-mvc/first-steps).

* To manually configure an existing project by using NuGet, see the [Adding Telerik UI through NuGet](https://www.telerik.com/aspnet-mvc/documentation/getting-started/setup-with-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:

  ```cshtml
  @using Kendo.Mvc.UI
  ```

Optionally, you can structure the document by adding the desired HTML elements like headings, divs, paragraphs, and others. In this tutorial, you will create a list that contains two items—one for each NumericTextBox. You will also add labels.

```Razor HtmlHelper
@using Kendo.Mvc.UI

<p class="title">Add new product</p>
<ul id="products-list">
    <li>
        <label for="currency">Price:</label>
        
    </li>
    <li>
        <label for="numeric">Currently in stock:</label>

    </li>
</ul>
```

## 2. Initialize the NumericTextBox

Use the NumericTextBox HtmlHelper  to add the component to a page. The `Name()` configuration method is mandatory as its value is used for the id and the name attributes of the NumericTextBox element.

```Razor HtmlHelper
@using Kendo.Mvc.UI

<p class="title">Add new product</p>
<ul id="products-list">
    <li>
        <label for="currency">Price:</label>
        @(Html.Kendo().NumericTextBox<decimal>()
            .Name("currency") // The name is required.
        )

    </li>
    <li>
        <label for="numeric">Currently in stock:</label>
        @(Html.Kendo().NumericTextBox<double>()
            .Name("numeric")
        )
    </li>
</ul>
```

> Do not set the `Name()` option when using `NumericTextBoxFor`. The `[WidgetName]For` method automatically sets the widget's `Name()` to the field it is bound to. For more information, see the [Fundamentals](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/helper-basics/fundamentals-mvc) article.

## 3. Configure the Displayed Values

The NumericTextBox provides a [variety of methods](https://www.telerik.com/aspnet-mvc/documentation/api/NumericTextBox) that allow you to configure its values. In this tutorial, you will perform the following configuration:

* To specify the number format for the control when it isn't focused, use the `Format()` method.
* To define what is the accepted minimal value, use the `Min()` method.
* To set the value, use the `Value()` method.
* To add a placeholder text in the NumericTextBox, use the `Placeholder()` method.

```Razor HtmlHelper
@using Kendo.Mvc.UI

<p class="title">Add new product</p>
<ul id="products-list">
    <li>
        <label for="currency">Price:</label>
        @(Html.Kendo().NumericTextBox<decimal>()
            .Name("currency")
            .Format("c") // Format the value as currency.
            .Min(0) // The minimal value is 0.
            .Value(30) // The current value is 30.
            .Placeholder("Enter product price") // Use the placeholder to display a hint.
        )

    </li>
    <li>
        <label for="numeric">Currently in stock:</label>
        @(Html.Kendo().NumericTextBox<double>()
            .Name("numeric")
            .Placeholder("Enter numeric value")
        )
    </li>
</ul>
```

## 4. Handle a NumericTextBox Event

The NumericTextBox exposes the `Change` and `Spin` events that you can handle and customize the component's functions. In this tutorial, you will use these events to log a new entry in the browser's console.

```Razor HtmlHelper
@using Kendo.Mvc.UI

<p class="title">Add new product</p>
<ul id="products-list">
    <li>
        <label for="currency">Price:</label>
        @(Html.Kendo().NumericTextBox<decimal>()
            .Name("currency")
            .Format("c")
            .Min(0)
            .Value(30)
            .Placeholder("Enter product price")
            .Events(e => e // Configure the client-side events.
                .Change("change")
                .Spin("spin")
            )
        )
    </li>
    <li>
        <label for="numeric">Currently in stock:</label>
        @(Html.Kendo().NumericTextBox<double>()
            .Name("numeric")
            .Placeholder("Enter numeric value")
        )
    </li>
</ul>

<script>
    function change() {
        console.log("Change :: " + this.value());
    }

    function spin() {
        console.log("Spin :: " + this.value());
    }
</script>
```

## 5. (Optional) Reference Existing NumericTextBox Instances

You can reference the NumericTextBox instances that you have created and build on top of their existing configuration:

1. Use the `id` attribute of the component instance to establish a reference.

   ```JS script
   <script>
       var numericTextBoxRef = $('#currency').data('kendoNumericTextBox'); // numericTextBoxRef is a reference to the existing currency instance of the helper.
   </script>
   ```

2. Use the [NumericTextBox client-side API](https://docs.telerik.com/kendo-ui/api/ui/numerictextbox#methods) to control the behavior of the widget. In this example, you will use the `readonly` method to disable user input.

   ```JS script
   <script>
       var numericTextBoxRef = $('#currency').data('kendoNumericTextBox'); // numericTextBoxRef is a reference to the existing currency instance of the helper.
       numericTextBoxRef.readonly(true); // Set the readonly method to true and disable user input.
   </script>
   ```

For more information on referencing specific helper instances, see the [Methods and Events](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/helper-basics/events-and-methods) article.

## Next Steps

* [Customizing the Appearance of the NumericTextBox](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/editors/numerictextbox/appearance)
* [Associating the label HTML element with the NumericTextBox](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/editors/numerictextbox/labels)
* [Setting a number Format in the NumericTextBox](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/editors/numerictextbox/formats)

## See Also

* [Using the API of the NumericTextBox for ASP.NET MVC (Demo)](https://demos.telerik.com/aspnet-mvc/numerictextbox/api)
* [NumericTextBox client-side API](https://docs.telerik.com/kendo-ui/api/ui/numerictextbox)
* [NumericTextBox server-side API](https://www.telerik.com/aspnet-mvc/documentation/api/numerictextbox)
* [Knowledge Base Section](https://www.telerik.com/aspnet-mvc/documentation/knowledge-base)
