Search results

NumericTextBoxFor and Model Binding in ASP.NET Core NumericTextBox control

05 May 2021 / 1 minute to read

This section demonstrates the Strongly typed extension support in NumericTextBox. The view which bind with any model is called as strongly typed view. You can bind any class as model to view. You can access model properties on that view. You can use data associated with model to render controls.

In this sample, first click the submit button to post the selected value in the MaskedTextBox. When posting the null value, validation error message will be shown below the NumericTextBox.

tagHelper
modelBinding.cs
Copied to clipboard
<div class="numeric-section">
    <form method="post">
        <ejs-numerictextbox id="numeric" name="value" ejs-for="@Model.value"></ejs-numerictextbox>
            <div id="errorMessage">
                <span asp-validation-for="value"></span>
            </div>
            <div id="submitbutton">
                <ejs-button id="submitButton" content="Submit"></ejs-button>
            </div>
    </form>
</div>
<style>
    .e-float-input.e-numeric.e-input-group {
        margin-top: 40px;
    }

    #errorMessage {
        color: red;
        text-align: center
    }

    #submitbutton {
        margin: 10px auto;
        text-align: center;
    }
</style>
Copied to clipboard
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using System.ComponentModel.DataAnnotations;

namespace EJ2CoreSampleBrowser.Controllers
{
    public class NumericValue
    {
        [Required]
        public int value { get; set; }

    }
    public partial class NumericTextBoxController : Controller
    {
        public ActionResult DefaultFunctionalities()
        {
            NumericValue val = new NumericValue();
            val.value = 10;
            return View(val);
        }
        [HttpPost]
        public ActionResult DefaultFunctionalities(NumericValue model)
        {
            NumericValue val = new NumericValue();
            val.value = model.value;
            return View(val);
        }
    }
}

Output be like the below.

NumericTextBox Sample