Using custom date formatting in the properties column of the associated Telerik MVC AJAX property

I am using the latest version of Telerik MVC in my ASP.NET MVC3 application with razor .

I defined the grid structure as follows:

 @(Html.Telerik() .Grid<GrantApplicationListViewModel>() .Name("grdGrantApplications") .Columns(column => { column.Bound(x => x.FullName) .Title("Owner") .Width(200); column.Bound(x => x.CreatedDate) .Title("Created") .Width(90); }) .DataBinding(dataBinding => dataBinding.Ajax().Select("AjaxGrantApplicationsBinding", "Home")) .Pageable(paging => paging.PageSize(30)) .TableHtmlAttributes(new { @class = "telerik-grid" }) ) 

My model looks like this:

 public class GrantApplicationListViewModel { public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string FullName { get { return FirstName + " " + LastName; } } public DateTime CreatedDate { get; set; } } 

I created a date format that I would like to use in my column to format the date:

 public static class DateTimeExtensions { public static string FormatDate(this DateTime instance) { return string.Format("{0:yyyy-MM-dd}", instance); } } 

How to use this formatting method in my column to format CreateDate? I tried the following:

 column.Bound(x => x.CreatedDate.FormatDate()) .Title("Created") .Width(90); 

.. and I get the following error:

 Bound columns require a field or property access expression. 
+4
source share
2 answers

you need to be attached to the property, so what you do will not work. What can you do:

 public class GrantApplicationListViewModel { public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string FullName { get { return FirstName + " " + LastName; } } public DateTime CreatedDate { get; set; } public DateTime FormattedDate{ get{ return FormatDate(CreatedDate)}; set; } } 

And then

 column.Bound(x => x.FormattedDate) .Title("Created") .Width(90); 

(The code is not syntactic, so you should clear it :))

you can also do

 column.Bound(x => x.FormattedDate) .Title("Created") .Format("{0:MM/dd/yyyy hh:mm tt}") .Width(90); 

If I'm not mistaken

+9
source

you can format date in GrantApplicationListViewModel class

 public DateTime CreatedDate { get { return string.Format("{0:yyyy-MM-dd}", this); } } 

This works for me. Or you can use this below.

 public DateTime CreatedDate { get { return string.Format("{0:yyyy-MM-dd}", DateOfJoining); } } 
+2
source

Source: https://habr.com/ru/post/1395381/


All Articles