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

MaskedTextBoxFor doesn't work for string model property

9 Answers 419 Views
MaskedTextBox
This is a migrated thread and some comments may be shown as answers.
Bobby
Top achievements
Rank 1
Bobby asked on 20 Mar 2014, 05:30 PM
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? ?

9 Answers, 1 is accepted

Sort by
0
Chris
Top achievements
Rank 1
answered on 21 Mar 2014, 03:55 AM
Hi,

I am having the exact same issue as Bobby.

Thanks,

Chris
0
Sandro
Top achievements
Rank 1
answered on 21 Mar 2014, 06:52 AM
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.
0
Chris
Top achievements
Rank 1
answered on 21 Mar 2014, 07:33 AM

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.

 

0
Georgi Krustev
Telerik team
answered on 24 Mar 2014, 10:08 AM
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.

 
0
Bill
Top achievements
Rank 1
answered on 15 Apr 2014, 04:35 PM
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.
0
Georgi Krustev
Telerik team
answered on 16 Apr 2014, 03:20 PM
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.

 
0
Bobby
Top achievements
Rank 1
answered on 18 Apr 2014, 07:03 PM
I had a chance to update to v2014.1.416 today and it appears that the reported issue in my OP has been resolved.
0
Ezequiel
Top achievements
Rank 2
answered on 30 Sep 2014, 11:54 AM
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") %>




0
Georgi Krustev
Telerik team
answered on 02 Oct 2014, 08:08 AM
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.

 
Tags
MaskedTextBox
Asked by
Bobby
Top achievements
Rank 1
Answers by
Chris
Top achievements
Rank 1
Sandro
Top achievements
Rank 1
Georgi Krustev
Telerik team
Bill
Top achievements
Rank 1
Bobby
Top achievements
Rank 1
Ezequiel
Top achievements
Rank 2
Share this question
or