Search code examples
jqueryasp.net-mvcasp.net-corebootstrap-datetimepicker

How to post time using bootstrap datetimepicker from view to controller


I am using bootstrap datetimepicker for date time functionality. While I am trying to post only 'Time' to controller from view the time is coming as '00:00:00' and not the actual selected the time is coming.

Below is my code Model

[DataType(DataType.Time)]
public TimeSpan StartTime { get; set; }

[DataType(DataType.Time)]
public TimeSpan EndTime { get; set; }

View

@Html.TextBoxFor(model => model.StartTime, new { @class = "form-control", @id = "txtStartTime",@name= "StartTime" })
@Html.TextBoxFor(model => model.EndTime, new { @class = "form-control", @id = "txtEndTime", @name = "EndTime" })

Code written in document.Ready function to show only 'Time' from datetimepicker

$('#txtStartTime').datetimepicker({ format: 'LT A' });
$('#txtEndTime').datetimepicker({ format: 'LT A' });

However, on form submit I am getting the value as '00:00:00' only, attached is the screenshot screenshot

Any help on this appreciated !


Solution

  • Beacuse you are using the wrong format

    $('#txtStartTime').datetimepicker({ format: 'hh:mm:ss'  });
    $('#txtEndTime').datetimepicker({ format: 'hh:mm:ss'  });
    

    If you want to use your format make StartTime and EndDate strings in your model and then parse them in Controller.