Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / Telerik MVC Extensions (superseded) > NumericTextBox > NumericTextBox and RangeAttribute

Not answered NumericTextBox and RangeAttribute

Feed from this thread
  • Posted on Dec 28, 2011 (permalink)

    Hello,

    I was expecting that the NumericTextBox (and CurrencyTextBox and IntegerTextBox) would use the RangeAttribute to determine the MinValue and MaxValue for the control. But this seems not to be the case.
    I have solved it now this way (as an example here my editor template for the decimal type):

    @model decimal?
     
    @{
        var minValue = decimal.MinValue;
        var maxValue = decimal.MaxValue;
        var attrAdapter = ViewData.ModelMetadata.GetValidators(ViewContext).OfType<RangeAttributeAdapter>().SingleOrDefault();
        if (attrAdapter != null)
        {
            var attr = (System.ComponentModel.DataAnnotations.RangeAttribute)attrAdapter.GetType()
                .GetProperty("Attribute", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static, nulltypeof(System.ComponentModel.DataAnnotations.RangeAttribute), new Type[0], null)
                .GetValue(attrAdapter, null);
            minValue = Convert.ToDecimal(attr.Minimum);
            maxValue = Convert.ToDecimal(attr.Maximum);
        }
    }
     
    @(Html.Telerik().NumericTextBox<decimal>()
            .Name(ViewData.TemplateInfo.GetFullHtmlFieldName(string.Empty))
            .InputHtmlAttributes(new { style = "width:100%" })
            .MinValue(minValue)
            .MaxValue(maxValue)
            .Value(Model)
    )
     
    

    But this does not feel good, to be forced to use reflection to get the information.
    What would be the preferred way to achieve this?

    Regards, Jaap

    Reply

  • Georgi Krustev Georgi Krustev admin's avatar

    Posted on Jan 3, 2012 (permalink)

    Hello,

     
    Range attribute is supported. Here is a test project, which shows that Range attribute works as expected. Check the http://localhost/Home/About page.

    Regards,
    Georgi Krustev
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the Telerik Extensions for ASP.MET MVC, subscribe to their blog feed now
    Attached files

    Reply

  • Posted on Jan 5, 2012 (permalink)

    Hello Georgi,

    After some digging into the code I found out that the RangeAttribute is supported in case you use the @Html.Telerik().IntegerTextBoxFor(expression) syntax, as you show in the test project. The Range attribute is accessed in the XxxFor methods.

    But it does not work when you have an edittemplate as this (like the Integer.cshtml delivered by default by Telerik):
    @model int 
    @(Html.Telerik().IntegerTextBox()
            .Name(ViewData.TemplateInfo.GetFullHtmlFieldName(string.Empty))
            .InputHtmlAttributes(new { style = "width:100%" })
            .MaxValue(int.MinValue)
            .MaxValue(int.MaxValue)
            .Value(Model)
    )

    Even if you remove the calls to MinValue and MaxValue it does not work (perhaps they would override the setting from the rangeattribute, but that's not the case)

    BTW: There is an error in the default Integer.cshtml edittemplate from Telerik: it calls MaxValue 2 times. First time should be MinValue.

    Regards, Jaap

    Reply

  • Georgi Krustev Georgi Krustev admin's avatar

    Posted on Jan 6, 2012 (permalink)

    Hello Jaap,

     
    Thank you for pointing the issue related with the Razor editor templates. I have updated them to use strongly-typed components as the WebForms editor templates do. As a gratitude for your involvement I have updated your Telerik points.

    As to the RangeAttribute, I will suggest you use the strongly-typed components and modify the editor template (or create custom for your needs) like so:

    @model int?
     
    @(Html.Telerik().IntegerTextBoxFor(m => m)
          .InputHtmlAttributes(new { style = "width:100%" })
    )
    The reported behavior is a known limitation. I will log your request to our intenal system and further will investigate how we can improve the behavior. As a side note calling Min/MaxValue method will override the RangeAttribute values.

    Regards,
    Georgi Krustev
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the Telerik Extensions for ASP.MET MVC, subscribe to their blog feed now

    Reply

  • Posted on Jan 6, 2012 (permalink)

    Hello Georgi,

    As I now see in an edittemplate you can also use the strong-typed version, so I think there is no reason to investigate the issue with RangeAttribute and the non-strong-typed version.
    Thank you for your information.

    Regards, Jaap

    Reply

Back to Top

Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / Telerik MVC Extensions (superseded) > NumericTextBox > NumericTextBox and RangeAttribute
Related resources for "NumericTextBox and RangeAttribute"

ASP.NET MVC NumericTextBox Features  |  Documentation  |  Demos  |  Telerik TV ]