【发布时间】:2013-09-21 03:15:22
【问题描述】:
我一直在与以下Lynda.com tutorial 合作学习 MVC 4 和 Razor。我一直试图让显示的时间只显示小时、分钟,然后是 AM/PM。截至目前,屏幕仍包含秒数(如下所示):
我尝试像this post about DateTime 这样格式化我的日期,但没有成功。现在我的函数中有以下代码,位于标题为“AuctionsController.vb”的控制器部分,similar to this post:
Function Auction() As ActionResult
Dim mainauction = New MVCAuction3.Models.Auctions
Dim ts As New TimeSpan(10, 0, 0)
mainauction.Title = "Example Auction"
mainauction.Description = "This is an example Auction"
mainauction.StartTime = DateTime.Now + ts
mainauction.EndTime = DateTime.Now.AddDays(7) + ts
mainauction.StartPrice = 1.0
mainauction.CurrentPrice = Nothing
ViewData("Auction") = mainauction
Return View()
End Function
这是 Razor 从“Auction.vbhtml”视图显示内容的方式:
<p>Start Time: @auction.StartTime.ToString() </p>
<p>End Time: @auction.EndTime.ToString()</p>
<p>Starting Price: @FormatCurrency(auction.StartPrice.ToString())</p>
修改:
这就是我在模态文件中声明时间变量的方式:
Private Property x_StartTime As DateTime
Private Property x_EndTime As DateTime
Public Property StartTime() As DateTime
Get
Return x_StartTime
End Get
Set(value As DateTime)
x_StartTime = value
End Set
End Property
Public Property EndTime() As DateTime
Get
Return x_EndTime
End Get
Set(value As DateTime)
x_EndTime = value
End Set
End Property
我还尝试从“Auction.vhtml”视图中获取以下内容,不幸的是,这给了我服务器错误,指示 “输入字符串的格式不正确。” :
<p>Start Time: @auction.StartTime.ToString("g") </p>
<p>End Time: @auction.EndTime.ToString("g")</p>
<p>Starting Price: @FormatCurrency(auction.StartPrice.ToString())</p>
我在没有格式化时间的 Razor 或 MVC 代码中做错了什么?非常感谢任何帮助!
【问题讨论】:
-
我没有看到任何尝试在您的代码中格式化时间?
-
@AntP 我继续并更新了我的问题,但我尝试在 Razor 中格式化字符串,但给了我一个服务器错误:“输入字符串的格式不正确。 "
-
除此之外 - 注意在 Web 应用程序中滥用
DateTime.Now。 Read more here. -
@MattJohnson 感谢您的文章! :) 在我弄清楚解决方案后,将不得不切换
DateTime.Now
标签: asp.net asp.net-mvc-4 datetime razor