New to Telerik UI for ASP.NET MVCStart a free 30-day trial

MaskedTextBox in Razor Pages

Razor Pages is an alternative to the MVC pattern that makes page-focused coding easier and more productive. This approach consists of a cshtml file and a cshtml.cs file (by design, the two files have the same name).

You can seamlessly integrate the Telerik UI MaskedTextBox for ASP.NET MVC in Razor Pages applications.

This article describes how to configure the MaskedTextBox component in a Razor Pages scenario.

For the complete project, refer to the MaskedTextBox in Razor Pages example.

Binding the MaskedTextBox to a PageModel Property

To bind the MaskedTextBox to a property from the PageModel, follow the next steps:

  1. Declare the PageModel at the top of the page.

    C#
        @page
        @model IndexModel
  2. Declare the widget either in a form or as a stand-alone widget:

    Razor
        @page
        @model IndexModel
    
        @inject Microsoft.AspNetCore.Antiforgery.IAntiforgery Xsrf
        @Html.AntiForgeryToken()
    
        <form method="post">
            <label for="phone_number">Phone number:</label>
            @(Html.Kendo().MaskedTextBoxFor(c=>c.PhoneNumber)
                        .Mask("(999) 000-0000")
                )
            <br />
            <input type="submit" name="name" value="Submit Form" />
        </form>
  3. Bind the property values in the backend:

    Razor
        public class IndexModel : PageModel
        {
            [BindProperty]
            public string PhoneNumber { get; set; }
    
            public void OnGet()
            {
                PhoneNumber = "555 123 4567"; // Assign value to the "PhoneNumber" property, if needed.
            }
    
            public void OnPost()
            {
                //omitted for clarity
            }
        }

See Also