I'm trying to use Kendo UI DatePicker
#(Html.Kendo().DatePickerFor(model => model.CalledInAt).Format("dd/MM/yyyy HH:mm tt").Value(DateTime.Now).HtmlAttributes(new { #style = "width:205px" }))
I'm using ASP.NET MVC 4 with entity framework.
when i try to submit a form it gives me the error. "The value '19/09/2013 04:08 AM' is not valid for CalledInAt"
If i change the format to dd/MM/yyyy then it works. something like below works.
#(Html.Kendo().DatePickerFor(model => model.CalledInAt).Format("MM/dd/yyyy HH:mm tt").Value(DateTime.Now).HtmlAttributes(new { #style = "width:205px" }))
what would be the reason?
Try to set the culture in web config that match with date format.
<globalization uiCulture="en" culture="en-AU" />
See this link ASP.NET Date time support for different cultures
Related
I have a Kendo MVC Grid that has a column with a DateTime column. Everything looks good and formats correctly. When i filter, it gives me a date picker and a time picker. When i remove the DateTimeFilter Template below and use template contains, it will give me a Date picker (which i want), but still wants to filter by the date and time.. Is there a way i can have the date and time all as the same field, but only filter with a Date picker.? Example: i use the Date picker to pick 07/24/2017 and it filter everything on that date regardless of time.. Or do they need to be completely different fields, or even concatenated fields in the same column.?
the Column data looks as such: 07/24/2017 18:12:00
columns.Bound(c => c.CreatedDate).Title("Submitted On")
.ClientTemplate("#= kendo.toString(kendo.parseDate(CreatedDate), 'MM/dd/yyyy HH:mm:ss') #")
.Filterable(ftb => ftb.Cell(cell => cell.Template("DateTimeFilter")));
If you're using MVC, in your model, you add DataType.Date above your DatePicker property as below:
[DataType(DataType.Date)]
public DateTime SubmittedOn{ get; set; }
Note: add reference to System.ComponentModel.DataAnnotations if its not included in your header.
Curently i am using this code then i submit my data to controller and through controller i add to my database
#Html.HiddenFor(model => model.date, new { #Value=DateTime.Now.ToShortDateString() })
If you do not want to show your time value, then you could do that by specifying the string format you want your DateTime to show in your ToString():
#Html.HiddenFor(model => model.date, new { #Value=DateTime.Now.ToString("dd-MM-yyyy") })
dd-MM-yyyy format will only show day-Month-year elements of the DateTime. To show hour-minutes-seconds, use HH-mm-ss or hh-mm-ss. For more info, check MSDN article on standard and custom date time format.
I need the default value of datetime picker to be empty.
#(Html.Kendo().DatePickerFor(model => model.RevisionDate)
.Name("RevisionDate")
)
"RevisionDate" is a datetime property in a asp.net mvc model.
how it's currently being displayed
and i need to display it as below
Your RevisionDate property is by default set to DateTime.MinValue which is 01/01/0001 ( all DateTime properties behave like that). This is why the Kendo UI DatePicker shows it like that. The solution is to make the property a nullable DateTime (DateTime?) whose default value is null.
If you don't want to make the property nullable, you can also do it like this:
#(Html.Kendo().DatePickerFor(m => m.Property).HtmlAttributes(new { value = "" }))
I had the same problem with a ComboBoxFor ComboBoxFor(m => m.LocationId).Value(Model.LocationId == 0 ? string.Empty : Model.LocationId.ToString()).
But I see that this approach doesn't work on the DatePickerFor(It could be a bug).
A workaround could be made in JS if($('#RevisionDate ').val() == "1/1/0001") { $('#RevisionDate ').val("") } but I would recommand Atanas's approach with a nullable field.
Blank out the field from the client side using something like this in your markup/cshtml file :
<script>
// Blank out the 1/1/0001 12:00 AM from the date field
$('#RevisionDate').val("");
</script>
New to Telerik Mvc, so I'm hoping I'm missing the obvious here. I am using version 2011.3.1115.340 of Telerik Mvc. The grid is being databound via ajax calls to controller actions and then the columns are hooked in to public properties. About as boilerplate as you can get.
Html.Telerik().Grid<ProductResult>()
.Name("Grid")
.DataBinding(databinding => databinding.Ajax()
.Select("GetProductInfo", "Product"))
.DataKeys(keys => keys.Add(a => a.ProductId))
.Columns(columns => {
columns.Bound(a => a.CreateDate).Width(30);
columns.Bound(a => a.Sales).Width(30);
columns.Bound(a => a.Service).Width(30);
columns.Bound(a => a.Training).Width(30);
columns.Bound(a => a.ModifiedDate).Width(30);
})
.Pageable(p => p.PageSize(30))
.Sortable()
.Filterable()
.Groupable()
All filtering, aside from date values, works fine. The bound class is ultimately delivered from a wcf service where the datetime values are formatted in the data contract
[DataMember]
[DisplayName("Last Modified Date")]
[DisplayFormat(DataFormatString = "{0:d}")]
public virtual DateTime ModifiedDate { get; set; }
When I use the built-in grid filter (dropdown) and enter a date value that I know exists in the datasource and can see in the grid, I get no results - an empty grid. I've read posts that seem to indicate that this should work like a charm, but obviously not in my case. Any idea what I'm doing wrong here?
columns.Bound(a => a.ModifiedDate).Format("{0:d}").Width(30);
I'm guessing your Date field "Modified Date" is a DateTime field. This is caused because the datepicker filter in KendoUI can't match up the time portion of your DateTime values. Since nothing exactly matches the values in your database, it returns nothing.
I recently battled this as well and my best solution was to cast the DateTime fields in SQL to a Date when I was selecting the fields with my SPROC. Yours would look something like this, assuming you are using SQL.
SELECT CAST(ModifiedDate AS date) AS ModifiedDate FROM YourTable
That fixed my problem of the filter as it was now returning the rows I wanted.
This exposed another problem for me as now my date values were all getting offset by minus six hours because we were hosting our site on Azure. To remedy that problem I followed exactly this from the Kendo team:
Kendo - Using UTC time on both client and server sides
When I edit my one of the form sin ASP.NET MVC 3 in this when I edit a user registration form then I got date in startdate is 21-Mar-12 12:00:00 AM in text box but I need 21-Mar-12.
So how can I format textbox date like that?
You could decorate the DateTime property on your view model with the [DisplayFormat] attribute which allows you to specify a given format:
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd-MMM-yy}")]
public DateTime SomeDate { get; set; }
and in your strongly typed view use the EditorFor helper to render the corresponding input field:
#Html.EditorFor(x => x.SomeDate)
As far as binding the value back to a DateTime field when posting back is concerned you could write a custom model binder that will use this format. I have shown an example here.