I am trying to add a new form to an existing ASP.NET MVC 5 application (.NET Framework). I have been unable to get the TimePicker control to return the value set in the UI. The control renders correctly and shows the values I would expect. But when I submit the form, the model's property value linked to the TimePicker is always all zeros or null. What am I doing wrong here?
Example code:
Model.cs
public class TestModel
{
public TimeSpan Start { get; set; }
}
TestController.cs
public class TestController : Controller
{
public ActionResult Index()
{
TestModel testModel = new TestModel();
testModel.Start = new TimeSpan(7, 30, 0);
return View("Schedule", testModel);
}
[HttpPost]
public ActionResult Schedule (TestModel schedule)
{
// I intend to save 'schedule' value to database here.
// but schedule.Start property is always [0, 0, 0]
// If TestModel.Start is defined as TimeSpan?, then the property is always null
...
return RedirectToAction(...);
}
}
Schedule.cshtml
@model TestModel
@{
Layout = "~/Views/Shared/_Layout.cshtml"
}
@using (Html.BeginForm("Schedule", "TestController", FormMethod.Post))
{
<div class="form-horizontal container-fluid" style="margin-left:15px;">
@Html.Label("Start Time")
@(Html.Kendo().TimePickerFor(m => m.Start).Name("Start").HtmlAttributes(new { @style = "width:100px;" }))
</div>
<div class="row">
<input type="submit" value="Save" class="btn btn-primary" />
</div>
</div>
}
I have tried a variety of different MVC ways to instantiate the TimePicker: TimePickerFor (as above) or just TimePicker, defining .Name() property or not, defining .Value(model.Start) or not, adding .Min & Max properties. The result in the Schedule controller is the same: no value assigned to any TimePicker control.
The other thing I tried was change the TestModel.Start property to DateTime. Doing that, the form submit is always short-circuited, apparently because of a model validation issue in the TImePicker control. Form focus always changes to the TimePicker control. I have not been able to view the actual validation error.
Hoping someone can identify the problem with my code. The TimePicker control seems ideal for the application I have in mind but if I can't get it working I'll have to do something else.
I am testing this code in VS 2022. The version of Kendo our application uses is older: v2022.1.301.
FYI, our application already uses a wide variety of Kendo controls including Grid, DateTimePicker, DropDownList and several others. We have not had trouble using those controls. But as far as I can tell, this is the first instance of TimePicker being added.
Thanks,
Jeff