在Telerik MVC AJAX绑定属性列上使用自定义日期格式化程序

Bre*_*ogt 4 c# asp.net asp.net-mvc asp.net-mvc-3 asp.net-mvc-2

Telerik MVC在我的ASP.NET MVC3应用程序上使用最新版本的控件razor.

我已经定义了我的网格结构如下:

@(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" })
)
Run Code Online (Sandbox Code Playgroud)

我的视图模型看起来像:

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; }
}
Run Code Online (Sandbox Code Playgroud)

我创建了一个日期格式化程序,我想在我的列中使用它来格式化日期:

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

如何在我的列中使用此格式方法来格式化CreatedDate?我尝试了以下方法:

column.Bound(x => x.CreatedDate.FormatDate())
     .Title("Created")
     .Width(90);
Run Code Online (Sandbox Code Playgroud)

..我得到以下错误:

Bound columns require a field or property access expression.
Run Code Online (Sandbox Code Playgroud)

Nea*_*alv 9

你必须绑定到一个属性,所以你正在做的事情是行不通的.你能做的是:

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; }
}
Run Code Online (Sandbox Code Playgroud)

然后

column.Bound(x => x.FormattedDate)
 .Title("Created")
 .Width(90);
Run Code Online (Sandbox Code Playgroud)

(代码语法不正确所以你必须清理:))

你也可以这样做

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

如果我没有记错的话