Having trouble getting help?
Contact Support
Contact Support
MaskedTextBoxFor and Model Binding
26 Oct 20223 minutes to read
This section demonstrates the Strongly typed extension support in MaskedTextBox. 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 MaskedTextBox.
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<div class="col-lg-12 control-section">
<div id="wrapper">
@Html.EJS().MaskedTextBoxFor(model => model.value).Width("200px").Created("onCreate").Render()
<div>
@Html.ValidationMessageFor(model => model.value)
</div>
<div id="submitbutton">
@Html.EJS().Button("btn").Content("Post").Render()
</div>
</div>
</div>
}
<script>
function onCreate() {
document.getElementById(this.element.id).setAttribute("name", "value");
}
</script>
<style>
#wrapper {
max-width: 246px;
margin: 30px auto;
padding-top: 50px;
}
#submitbutton {
margin-top: 20px;
margin-left: 65px;
}
#control-content #wrapper .field-validation-error {
color: red;
}
</style>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using System.ComponentModel.DataAnnotations;
namespace EJ2CoreSampleBrowser.Controllers.MaskedTextbox
{
public class MaskValue
{
[Required]
public string value { get; set; }
}
public partial class MaskedTextboxController : Controller
{
public ActionResult DefaultFunctionalities()
{
MaskValue val = new MaskValue();
val.value = "10";
return View(val);
}
[HttpPost]
public ActionResult DefaultFunctionalities(MaskValue model)
{
MaskValue val = new MaskValue();
val.value = model.value;
return View(val);
}
}
}
Output be like the below.