MaskedTextBoxFor doesn't work for string model property

10 posts, 0 answers
  1. Bobby
    Bobby avatar
    24 posts
    Member since:
    Nov 2013

    Posted 20 Mar 2014 Link to this post

    I have a view model like:
    1.public class MyViewModel
    2.{
    3.    public string PhoneModel { get; set; }
    4.}

    That is currently rendered on my view as:
    1.@model MyViewModel
    2. 
    3.@Html.EditorFor(model => model.PhoneNumber)

    With yesterday's official release of the MaskedTextBox control, I am attempting to update my view to use it instead like:
    1.@model MyViewModel
    2. 
    3.@(Html.Kendo().MaskedTextBoxFor(model => model.PhoneNumber).Mask("(999) 000-0000"))

    Unfortunately, VS is complaining "Cannot implicitly convert type 'string' to 'int?'" and the view throws the same compilation error.

    Looking at the following WidgetFactory metadata, the MaskedTextBoxFor is trying to return its value as an int?
    1.//
    2.// Summary:
    3.//     Creates a new Kendo.Mvc.UI.MaskedTextBox.
    4.public virtual MaskedTextBoxBuilder MaskedTextBoxFor(Expression<System.Func<TModel, int?>> expression);

    Phone numbers are typically too big of numbers to be represented by int, not that I would want to incur the overhead of doing so even if it was possible.  Are there any plans to update the helper to return the model's data type rather than hard-coding the return type to int? ?
  2. Chris
    Chris avatar
    2 posts
    Member since:
    Nov 2012

    Posted 20 Mar 2014 in reply to Bobby Link to this post

    Hi,

    I am having the exact same issue as Bobby.

    Thanks,

    Chris
  3. UI for ASP.NET MVC is VS 2017 Ready
  4. Sandro
    Sandro avatar
    2 posts
    Member since:
    Aug 2008

    Posted 21 Mar 2014 Link to this post

    Don't use MaskedTextBoxFor but MaskedTextBox - however you will not be happy with the output. The whole widget is kind of flakey and useless!

    I've tried to use it in conjunction with strings (Placeholder will end up in the viewmodel - together with number entered) and doubles (1.0 will be displayed as 1__,_ with a mask of 000.0 - tried even 999.9).
    The readonly-attribute (which works with a normal textbox) will be ignored + when you place the focus to the widget the cursor inside the textbox is at the end while it would make more sense if the cursor would be at the beginning.

    We generally like the idea of the masked textbox but currently it's completely unusable.
  5. Chris
    Chris avatar
    2 posts
    Member since:
    Nov 2012

    Posted 21 Mar 2014 in reply to Sandro Link to this post

    Yip. Also tried MaskedTextBox and although it "works" I've come to the same conclusion that the control won't work for me for the reasons you list. Pity.

     

  6. Georgi Krustev
    Admin
    Georgi Krustev avatar
    3706 posts

    Posted 24 Mar 2014 Link to this post

    Hello guys,

    Thank you for drawing our attention to this bug. It will be addressed in the next internal build of Telerik UI for ASP.NET MVC scheduled for this week. Bobby I updated your Telerik points because of the bug report.

    With regards to other comments, the MaskedTextBox is designed to work with string values. That is why numbers are placed in the beginning of the input element.
    When a readonly attribute is added, the widget will become readonly too. It behaves the same way as a normal input element does. If the widget is readonly and empty, on focus it will show the empty mask (the caret will or won't be shown depending on the browser). If the widget is readonly and not empty, then the whole text will be selected on focus. The widget behaves the same way when it is editable and not empty. The last case is when the widget is editable and empty. In this case the caret position will be persisted on focus.

    Sandro, could you elaborate more why the widget is unusable in current state?

    As a general suggestion, you can share your feedback in our UserVoice portal, which will help us to improve behavior of our widgets.

    Regards,
    Georgi Krustev
    Telerik
     

    Build cross-platform mobile apps using Visual Studio and .NET. Register for the online webinar on 03/27/2014, 11:00AM US ET.. Seats are limited.

     
  7. Bill
    Bill avatar
    75 posts
    Member since:
    Feb 2013

    Posted 15 Apr 2014 Link to this post

    Another issue I've found with the MaskedTextBoxFor is that if you are using regular expression validators via view model attributes & unobtrusive, they no longer work with empty strings.  It appears to be a timing issue when the textbox loses focus.  The mask is still present when the unobtrusive validation is performed on it so validation fails.  Kendo removes the mask but it appears to be after validation is performed.

    I'm sure there's some javascript hack I could put together to validate after a second delay or something, but its not worth it.  Between the issues mentioned above and now this, I'm not going to use it the control for now.
  8. Georgi Krustev
    Admin
    Georgi Krustev avatar
    3706 posts

    Posted 16 Apr 2014 Link to this post

    Hello Bill,

    Could you please elaborate more on this issue? Is it possible to provide more information about the used regular expression and when and how the issue is reproduced. A working demo will be of a great help.
    Looking forward to hearing from you.

    Regards,
    Georgi Krustev
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  9. Bobby
    Bobby avatar
    24 posts
    Member since:
    Nov 2013

    Posted 18 Apr 2014 Link to this post

    I had a chance to update to v2014.1.416 today and it appears that the reported issue in my OP has been resolved.
  10. Ezequiel
    Ezequiel avatar
    36 posts
    Member since:
    Oct 2011

    Posted 30 Sep 2014 Link to this post

    I have this EditorTemplate
    <%= Html.Kendo().MaskedTextBox()
                    .Name(this.ViewData.TemplateInfo.GetFullHtmlFieldName(string.Empty))
                    .Value(this.Model)
                    .Mask("LLL0000") %>

    When I try to create a new item in a grid, it's displayed as nul___, as you can see at the attached image.
    I even tried the EditorTemplate the following way, but got the same result:
    <% string value = string.Empty;
       if (this.Model != null)
       {
           value = this.Model;
       }  %>
     
    <%= Html.Kendo().MaskedTextBox()
                    .Name(this.ViewData.TemplateInfo.GetFullHtmlFieldName(string.Empty))
                    .Value(value)
                    .Mask("LLL0000") %>




  11. Georgi Krustev
    Admin
    Georgi Krustev avatar
    3706 posts

    Posted 02 Oct 2014 Link to this post

    Hello Ezequiel,

    Thank you for drawing out attention to this issue. The MaskedTextBox widget incorrectly converts null value into a "null" string and outputs it in the input element. We will address this problem for the upcoming release of Kendo UI due the mid of next week. You can follow the status of the issue here.
    I updated your Telerik points because of the involvement.

    Regards,
    Georgi Krustev
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
Back to Top
UI for ASP.NET MVC is VS 2017 Ready