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

Bug with EditorFor when EditorTemplate uses DatePicker

3 Answers 92 Views
Date/Time Pickers
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Shane Milton
Top achievements
Rank 2
Shane Milton asked on 05 Aug 2010, 03:40 AM
We have a problem when we use the DatePicker inside an EditorTemplate. The problem is that the name that it assumes for all instances after the first instance is identical to the first instance's name, which causes the javascript to break.

Our ViewModel:
public class Foo
{
    DateTime? StartDate { get; set; }
    DateTime? EndDate { get; set; }
}

Our View:
<%= Html.EditorFor(m => m.StartDate, "General/DateTime") %>
<%= Html.EditorFor(m => m.EndDate, "General/DateTime") %>

Our EditorTemplate:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<System.DateTime?>" %>
<%= Html.Telerik().DatePicker()
                .Value((Model.HasValue ? Model.Value : DateTime.Today))
                .ShowButton(true) %>


I've put in a temporary work-around where I force the Name in the EditorTemplate to something unique (Name="Foo" + Model.GetHashCode() will do it) but this is obviously a terrible idea as it breaks validators and such. However, it's sufficient enough to prove that the DatePicker has issues naming itself.

Any solid workarounds or fixes for this?

3 Answers, 1 is accepted

Sort by
0
Shane Milton
Top achievements
Rank 2
answered on 05 Aug 2010, 03:48 AM
And of course, as soon as I post this and give up, I find a solution:
http://demos.telerik.com/aspnet-mvc/datepicker/edittemplate?theme=vista


So curious.. Why was my above attempt failing? Is this a bug in the DatePicker or is this an MVC limitation?
0
Georgi Krustev
Telerik team
answered on 05 Aug 2010, 08:07 AM
Hi Shane Milton,

As you know ASP.NET MVC framwork does not handle controls' id. Hence you need to generate id of the datepicker by hand. In this chain of thoughts I must say that this is not a bug.

Actually I checked your editor template, but I could not seen the set name using Name(,,,) method. As you know it is obligatory. Did you remove it when post the sample code?

Best wishes,
Georgi Krustev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Denis
Top achievements
Rank 1
answered on 03 Oct 2012, 09:27 PM
Just use the following line as an argument to Name() function

@ViewData.TemplateInfo.HtmlFieldPrefix

you may also prefix or postfix it with arbitrary string if you already use the appropriate names, here's my code for instance:

@if ( Model.HasValue )
{
    @Html.Kendo().DateTimePickerFor(model => Model.Value).Name(@ViewData.TemplateInfo.HtmlFieldPrefix)
}
else
{
    @Html.Kendo().DateTimePicker().Name(@ViewData.TemplateInfo.HtmlFieldPrefix)
}
Tags
Date/Time Pickers
Asked by
Shane Milton
Top achievements
Rank 2
Answers by
Shane Milton
Top achievements
Rank 2
Georgi Krustev
Telerik team
Denis
Top achievements
Rank 1
Share this question
or