【问题标题】:Using a custom date formatter on a Telerik MVC AJAX bound property column在 Telerik MVC AJAX 绑定属性列上使用自定义日期格式化程序
【发布时间】:2012-02-08 10:05:53
【问题描述】:

我在我的ASP.NET MVC3 应用程序上使用最新版本的Telerik MVC 控件和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" })
)

我的视图模型如下所示:

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 static class DateTimeExtensions
{
     public static string FormatDate(this DateTime instance)
     {
          return string.Format("{0:yyyy-MM-dd}", instance);
     }
}

如何在我的专栏中使用这种格式化方法来格式化 CreatedDate?我尝试了以下方法:

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

..我收到以下错误:

Bound columns require a field or property access expression.

【问题讨论】:

    标签: c# asp.net asp.net-mvc asp.net-mvc-3 asp.net-mvc-2


    【解决方案1】:

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

    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; }
    }
    

    然后

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

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

    你也可以

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

    如果我没记错的话

    【讨论】:

    • 我可以这样做,但这不应该留给视图来格式化吗?
    • 是的,但这意味着更复杂的代码,您将不得不覆盖“绑定”功能......所以最简单的方法是 atm 添加一个属性。我在某个地方找到了您也可以执行 .Format("{0:MM/dd/yyyy hh:mm tt}") 的地方,也许可以先尝试一下(但不确定是否可行)
    • 是否可以使用我的 Format 方法,因为我想在站点范围内使用它,如果必须更改日期格式,那么我只想更改一次。
    • 您应该设置一个返回格式化字符串的全局属性。像 public string DateFormatString { get { return "{0:MM/dd/yyyy hh:mm tt}"; } } 然后 .Format(DateFormatString())
    • @BrendanVogt 你不能把它留给视图,因为 Kendo Grid 是在 JavaScript 中运行的,而不是 C#。想象一下,您单击页面上的一个按钮,将新数据加载到网格中 - JavaScript 如何在其上运行 C# 函数? Kendo 确实支持诸如“客户端模板”之类的东西,它是在 JavaScript 中定义的,并且可以执行诸如格式化之类的事情。
    【解决方案2】:

    你可以在GrantApplicationListViewModel类中格式化日期

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

    这对我有用。或者你可以通过以下方式使用

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

    【讨论】:

    • 我可以这样做,但这不应该留给视图来格式化吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-12
    • 2021-08-12
    • 1970-01-01
    相关资源
    最近更新 更多