Epoch/Unix Razor 视图中日期时间的时间戳(以毫秒为单位)
Epoch/Unix timestamp (in ms) to Datetime in Razor View
我有一个 .Net Core MVC 项目,我在其中使用 pagination 作为定义的 pr。 Microsoft 文档。
因为是这种情况,而且由于我的数据库时间戳在 Epoch 中,我需要将时间戳转换为日期时间对象,因为它用于我的 Razor 视图中的 table。 table创建如下:
@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 Pagination 文档分页需要我使用我的模型,因为它在我滚动分页时请求数据。
我认为可以这样做:
<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>
我的表格 header 下的部分允许我在我的模型中定义我的 header。如下:
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; }
}
我有一个 .Net Core MVC 项目,我在其中使用 pagination 作为定义的 pr。 Microsoft 文档。
因为是这种情况,而且由于我的数据库时间戳在 Epoch 中,我需要将时间戳转换为日期时间对象,因为它用于我的 Razor 视图中的 table。 table创建如下:
@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 Pagination 文档分页需要我使用我的模型,因为它在我滚动分页时请求数据。
我认为可以这样做:
<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>
我的表格 header 下的部分允许我在我的模型中定义我的 header。如下:
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; }
}