Razor 视图中的纪元/Unix 时间戳(以毫秒为单位)到日期时间



我有一个.Net Core MVC项目,其中我正在使用Microsoft文档中定义的分页。

在这种情况下,由于我的数据库时间戳在 Epoch 中,我需要将时间戳转换为日期时间对象,因为它用于我的 Razor 视图中的表。该表的创建方式如下:

@model PaginatedList<MSPFrontend.Tripmetadata>
<div class="table-responsive">
<table class="table table-bordered table-condensed table-hover table-striped">
<thead>
<tr>
<th>Trip Id</th>
<th>Start Time</th>
<th>End Time</th>
<th>Duration</th>
<th>Avg Speed</th>
<th>Distance</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr class="trip" data-id="@item.Tripid" data-url="@Url.Action("TripMapping", "Trip")">
<td>@item.Tripid</td>
<td>@item.Starttimestamp</td>
<td>@item.Endtimestamp</td>
<td>@item.Duration</td>
<td>@item.AvgSpeed knots</td>
<td>@item.Distance km</td>
</tr>
}
</tbody>
</table>
</div>

我的模型如下所示:

public partial class Tripmetadata
{
public Tripmetadata()
{
}
[Key]
public int Tripid { get; set; }
[Display(Name = "Start Timestamp")]
public long? Starttimestamp { get; set; }
[Display(Name = "End Timestamp")]
public long? Endtimestamp { get; set; }
public long? Duration { get; set; }
[Display(Name = "Average Speed")]
public decimal? AvgSpeed { get; set; }
public decimal? Distance { get; set; }
}

我以前使用 ViewModel 来处理这个问题,但作为 pr. Microsoft 分页文档,分页需要我使用我的模型,因为它在我滚动分页时请求数据。

我认为可以做这样的事情:

<td>@new DateTime(1970, 1, 1, 0, 0, 0).AddMilliseconds(@Convert.ToDouble(@item.Starttimestamp))</td>

但是当我尝试时,我得到以下结果:

jquery.js:9566 GET http://localhost:1048/Trip/TripTable 500 (Internal Server Error)

我设法找到了我的解决方案,实际上非常简单。

在我的@foreach(var item in Model)里,我可以进行转换,即使这可能不是最好的方法。

在我看来,我最终有以下几点:

<div class="table-responsive">
<table class="table table-bordered table-condensed table-hover table-striped">
<thead>
<tr>
@{ 
var headerItem = Model[1];
}
<th>@Html.DisplayNameFor(modelItem => headerItem.Tripid)</th>
<th>@Html.DisplayNameFor(modelItem => headerItem.Starttimestamp)</th>
<th>@Html.DisplayNameFor(modelItem => headerItem.Endtimestamp)</th>
<th>@Html.DisplayNameFor(modelItem => headerItem.Duration)</th>
<th>@Html.DisplayNameFor(modelItem => headerItem.AvgSpeed)</th>
<th>@Html.DisplayNameFor(modelItem => headerItem.Distance)</th>
</tr>
</thead>
<tbody>
@foreach(var item in Model)
{
var startTime = new DateTime(1970, 1, 1, 0, 0, 0).AddMilliseconds(Convert.ToDouble(@item.Starttimestamp));
var endTime = new DateTime(1970, 1, 1, 0, 0, 0).AddMilliseconds(Convert.ToDouble(@item.Endtimestamp));
double diff = Convert.ToDouble(item.Endtimestamp) - Convert.ToDouble(item.Starttimestamp);
var duration = TimeSpan.FromMilliseconds(diff);
var avgSpeed = Math.Truncate(100 * Convert.ToDecimal(item.AvgSpeed)) / 100;
var distance = Math.Truncate(100 * Convert.ToDecimal(item.Distance)) / 100;
<tr class="trip" data-id="@item.Tripid" data-url="@Url.Action("TripMapping", "Trip")">
<td>@item.Tripid</td>
<td>@startTime</td>
<td>@endTime</td>
<td>@duration</td>
<td>@avgSpeed knots</td>
<td>@distance km</td>
</tr>
}
</tbody>
</table>
</div>

表格标题下的零件允许我在模型中定义标题。如下:

public partial class Tripmetadata
{
public Tripmetadata()
{
}
[Key]
[Display(Name = "Trip ID")]
public int Tripid { get; set; }
[Display(Name = "Start Timestamp")]
public long? Starttimestamp { get; set; }
[Display(Name = "End Timestamp")]
public long? Endtimestamp { get; set; }
[Display(Name = "Duration")]
public long? Duration { get; set; }
[Display(Name = "Average Speed")]
public decimal? AvgSpeed { get; set; }
[Display(Name = "Distance")]
public decimal? Distance { get; set; }
}

最新更新