Multiple DateTimeInput in a view

Jul 3, 2012 at 10:12 AM


I can't get multiple DateTimeInput in a view to all work properly.
The simplest example I've tried is

@Html.DateTimeFor(model => Model.StartDate, DateTime.Now).Date();
@Html.DateTimeFor(model => Model.Test, DateTime.Now).Date();	

The inputs for Model.Startdate works as expected, but the drop downs for month and date for Model.Test just contains the current month and date.
Both model values are null when the view loads.

Jul 3, 2012 at 8:50 PM
Edited Jul 3, 2012 at 8:50 PM

First I don't think your date are null since the overload you are using is for DateTime not for DateTime?. Probably the field is empty in wich case it contains default(DateTime)....However, default(DateTime) is dealt with as if it were this should not change the behaviour of the control.

The problem you are experincing might be due to some DateRange attribute that is applied on StartDate and/or on TestDate that limits the allowed dates, or worst to the fact that the DateTime.Now is out from from the minimum and maximum dates specified by this DateRange attribute. Please verify and let me know...with no DateRange attribute your example works for me

Jul 4, 2012 at 7:11 AM

Of course, your right. They contain the default DateTime.

I don't have any attributes at all in the model:

public class TestModel
	public DateTime StartDate { get; set; }
	public DateTime Test { get; set; }

When the view is loaded, there's an error in the Firebug console, saying "CurrYear is not defined".

I'm using MVC 4, if that could make a difference.

Jul 4, 2012 at 11:51 AM

I made a test with a similar model without initializing neither of the two dates so both of them contain default(DateTime)...and it worked properly.

mmm Now we are in Mvc4 Rc while the Mvc Controls Toolkit 2.1 is targetted for the Mv4 beta. Today I released the version 2.2 that is compatible with Mvc4 RC.

However, that the problem should not come from this version mismatch...since it is mainly a javascript problem.....Probably due to the new bundling feature of the put the js file in the wrong order...or maybe you included some js file twice ...check this!

Jul 5, 2012 at 6:58 AM
Edited Jul 5, 2012 at 6:59 AM

Confirmed that this is a javascript include order problem.
I was using the bundling feature, but changed to:

<script src="@Url.Content("~/Scripts/MVCControlToolkit/MVCControlToolkit.Controls-2.0.0.js")"></script>
<script src="@Url.Content("~/Scripts/MVCControlToolkit/MVCControlToolkit.Controls.Datetime-2.0.0.js")"></script>

and it works fine now.