This is a migrated thread and some comments may be shown as answers.

How to set a default value for NumericTextBoxFor HtmlHelper

3 Answers 1911 Views
NumericTextBox
This is a migrated thread and some comments may be shown as answers.
Shawn
Top achievements
Rank 1
Shawn asked on 27 Feb 2019, 07:54 PM

Hello,

I've been searching all over the Internet for an answer to this question, but to no avail.  How do I set a default value for a NumberTextBoxFor HtmlHelper?  I've seen examples where it's done by something similar to this: .HtmlAttributes(new { @Value = "9"}). However, this doesn't for for me.  Any help is appreciated.  Thanks.

Shawn A.

3 Answers, 1 is accepted

Sort by
0
Georgi
Telerik team
answered on 04 Mar 2019, 02:52 PM
Hello Shawn,

Generally speaking the NumericTextBox creates a numeric which is bound to the model. In other words a value attribute is generated which value matches the value of the model. Therefore, I would suggest to set the default value in the model.

Nevertheless, you could set a default value to the numeric using the Value configuration.

e.g.

@(Html.Kendo().NumericTextBoxFor(x=> x.Field).Value(5))


Regards,
Georgi
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Shawn
Top achievements
Rank 1
answered on 04 Mar 2019, 04:26 PM

Hi Georgi,

I was able to set the default value in the "DataSource" section for the Grid.  For some reason when I tried it as you suggested the default values would not display.  Perhaps my issue was due to the fact that the NumberTextBoxFor was inside a Partial View template for a Grid's edit mode that was set to 'PopUp'.  Thanks!

.DataSource(d => d
     .Model(m =>
     {
          m.Field(p => p.MyField).DefaultValue(1234);
     })

0
Georgi
Telerik team
answered on 07 Mar 2019, 11:37 AM
Hello Shawn,

Indeed it is necessary to set the default value to the model as on the client the numeric is bound to the model which means that even if the widget has a default value, it is overridden by the value from the model.


Regards,
Georgi
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
NumericTextBox
Asked by
Shawn
Top achievements
Rank 1
Answers by
Georgi
Telerik team
Shawn
Top achievements
Rank 1
Share this question
or