如何格式化自动生成的 WebGrid 日期列

How to format automatically generated WebGrid date columns

我看到有很多解释如何单独格式化 DateTime WebGrid 列的答案,但是是否可以设置 WebGrid 自动格式化所有类型的列DateTime?

我问是因为我的 WebGrid 自动从我的 classes 中提取数据,所以我实际上从未访问过 webgrid.Column,因为我正在使用(尝试) 通用 WebGrid 视图:

@model IEnumerable<MVCQCPage.Helpers.IGriddable>

@{
    var grid = new WebGrid(new List<object>());
    var linkPrefix = string.Empty;
    var selectable = false;

    if (Model.Count() > 0)
    {
        string[] columnNames = { };

        var firstItem = Model.First();

        linkPrefix = firstItem.RowLinkPrefix;
        columnNames = firstItem.ColumnHeaders;
        selectable = firstItem.Selectable;

        grid = new WebGrid(Model,
            rowsPerPage: 100,
            columnNames: columnNames);
    }
}
<div id="grid">
    @grid.GetHtml(
            tableStyle: "table",
            alternatingRowStyle: "alternate",
            headerStyle: "header"
        )
</div>

其中 IGriddable 看起来像这样:

public interface IGriddable
{
    string RowLinkPrefix { get; }
    string[] ColumnHeaders { get; }
    bool Selectable { get; }
}

因此每个实现它的 class 可能包含如下内容:

public string[] ColumnHeaders { get; } = new string[] { "SampleNo", "QCDate", "StatusClerk", "StatusSupervisor" };
public string RowLinkPrefix { get { return $"/receiving/{Pallet.Grv.GRVNo}/{Pallet.PalletSeq}/"; } }
public bool Selectable { get; } = true;

我尝试将 Displayformat 添加到 QCDate 属性,但这没有帮助。

非常感谢任何帮助或建议,谢谢!

我设法通过修改我的属性来解决这个问题:

public DateTime QCDate { get; set; }
public string Date { get { return QCDate.ToString("dd/MM/yyyy"); } } 

并将 ColumnHeaders 更改为

public string[] ColumnHeaders { get; } = new string[] { "SampleNo", "Date", "StatusClerk", "StatusSupervisor" };

因此,当此项在网格中显示时,它现在将显示我需要的短日期字符串。