NumericTextbox null value

Hi SF,

Is there a way for NumericTextbox to be optional?, in other words sending a null value to my model when they clear it out / leave it blank. Right now if you leave it blank it just sends "0" as the value.

Best,

 

7 Replies

SP Sureshkumar P Syncfusion Team February 27, 2020 12:23 PM UTC

Hi Juan, 
 
Greetings from Syncfusion support. 
 
Based on your shared information. We suspect that you have get the model value as zero while post the numeric component value. We suggest you set the nullable int type for the value property variable to resolve the facing issue.  
 
Kindly refer the below code example. 
[index.cshtml] 
 
@using Syncfusion.EJ2 
@model WebApplication1.Controllers.NumericValue 
    @using(Html.BeginForm()) 
{ 
@Html.ValidationSummary(true) 
<div class="col-lg-12 control-section"> 
    <div id="wrapper"> 
        @Html.EJS().NumericTextBoxFor(model => model.value).Width("200px").Render() 
        <div> 
            @Html.ValidationMessageFor(model => model.value) 
        </div> 
        <div id="submitbutton"> 
            @Html.EJS().Button("btn").Content("Post").Render() 
        </div> 
    </div> 
</div> 
} 
[controller.cs] 
 
public class NumericValue 
    { 
        [Required] 
        public int? value { get; set; } 
 
    } 
    public class HomeController : Controller 
    { 
        public ActionResult Index() 
        { 
            NumericValue val = new NumericValue(); 
            val.value = 10; 
            return View(val); 
        } 
 
        [HttpPost] 
        public ActionResult Index(NumericValue model) 
        { 
            NumericValue val = new NumericValue(); 
            val.value = model.value; 
            return View(val); 
        } 
 
Please refer the below screen shot: 
 
 
 
We have prepared the sample based on your requirement. please find the sample here: https://www.syncfusion.com/downloads/support/directtrac/general/ze/WebApplication11339691203  
 
Regards, 
Sureshkumar P 



JJ Juan Jiminez February 27, 2020 05:32 PM UTC

Hi Sureshkumar,

Thanks for the answer. Is there a way to achieve this with a string model?, I'm using this same model to bind multiple types of inputs dynamically, some are NumericTextbox, some ColorPicker, etc. So I'm not even using NumericTextBoxFor() but just NumericTextBox() and then I bind my model based on the Input's name attribute.

Best,


SP Sureshkumar P Syncfusion Team February 28, 2020 08:41 AM UTC

Hi Juan, 
 
Thanks for your update. 
 
Yes, we can use string model to set the value to the component. Kindly refer the below code example. 
 
[index.cshtml] 
 
@using Syncfusion.EJ2 
@using Syncfusion.EJ2.Inputs 
@using Syncfusion.EJ2.Buttons 
@model WebApplication1.Controllers.DateRange 
 
 
@{ 
    IDictionary<string, object> htmlName = new Dictionary<string, object>(); 
    htmlName.Add("name", "rangevalue"); 
} 
 
<form method="post"> 
    @Html.EJS().NumericTextBox("numeric").HtmlAttributes(htmlName).Value(@Model.rangevalue).Render() 
    @Html.EJS().Button("btn").Content("submit").Render() 
</form> 
 
[controller.cs] 
public class DateRange 
    { 
        [Required(ErrorMessage = "Please enter the range value")] 
        public string rangevalue { get; set; } 
    } 
 
    public class HomeController : Controller 
    { 
        public ActionResult Index() 
        { 
            DateRange tx = new DateRange(); 
            tx.rangevalue = "2"; 
            return View(tx); 
        } 
        [HttpPost] 
        public ActionResult Index(DateRange DM) 
        { 
            DateRange tx = new DateRange(); 
            tx.rangevalue = DM.rangevalue; 
            return View(tx); 
        } 
} 
 
 
Please find the below screen shot  
 
 
We suggest you use NumericTextboxFor control to manipulate the form component actions in the MVC platform and also use number type model instead of string model that will give you better solution. 
 
Regards, 
Sureshkumar P 



JJ Juan Jiminez February 28, 2020 01:33 PM UTC

Hi Sureshkumar,

That's how I'm mapping it right now (to a string model), but then null / blank values are returned as "0" by the numeric textbox. Does it mean there's no way to get back the null values other than mapping it to a nullable int?


Best, 


SP Sureshkumar P Syncfusion Team March 2, 2020 08:40 AM UTC

Hi Juan, 
 
Thanks for your update.  
 
We can also set the string type model value to the numerictextbox and get the value as null when post back. Please find the screen shot and sample here. 
 
Screen shot:  
 
 
 
Kindly check the above solution and let us know it meets your requirement. 
 
Regards, 
Sureshkumar P 



JJ Juan Jiminez March 2, 2020 08:16 PM UTC

Thanks Sureshkumar,

It is worked as expected with the latest version (mapping null values to string).




SP Sureshkumar P Syncfusion Team March 3, 2020 07:20 AM UTC

Hi Juan, 
 
Thanks for your update. Please get back to us if you need any further assistance on this. 
 
Regards, 
Sureshkumar P 


Loader.
Up arrow icon