我正在编写一个简单的 ASP.Net 页面,其中有几个字段,其中一个用于超时。我想将它显示为毫秒(但仍然希望有一个时间跨度而不是 int/string)。我正在编写以下代码:
<input asp-for="Entry.Interval" asp-format="{0:fff}" type="text" class="form-control">
但是这里有个问题。此格式未按预期工作。我希望 TimeSpan.FromMinutes(2).ToString("fff")
返回 120000
,但它返回 000
。这显然是因为 TimeSpan
使用了 Milliseconds
属性,在此示例中该属性为零,但我需要 TotalMilliseconds
。
是否有某种格式强制以所需单位显示整个 TimeSpan
?我真的不想写一个整数字段并手动将其映射到这个 TimeSpan
上。
最佳答案
您可以定义另一个仅用于绑定(bind)的属性
class Entry
{
public TimeSpan Interval { get; set; }
public int IntervalMS
{
get { return (int)Interval.TotalMilliseconds; }
set { Interval = TimeSpan.FromMilliseconds(value); }
}
//other stuff...
}
然后
<input asp-for="Entry.IntervalMs" type="text" class="form-control">
关于c# - 将时间跨度格式化为 TotalMilliseconds,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39204625/