MVC3& Razor - 将日期时间字符串从“mm/dd/yyyy 12:00:00 AM”更改为至“mm/dd/yyy”在文本框中
我试图阻止时间显示在使用 DateTime 的生日文本字段中
我的代码:(我正在使用 Jquery DatePicker)
<label for="birthday">Birthday:</label>
@Html.TextBox("birthday", (Model.Birthday.ToString()), new { @class = "datePicker" })
Javascript:
$(document).ready(function () {
$('.datePicker').datepicker({ showOn: 'both', buttonImage: "/content/images/calendar-red.gif" });
});
我有日期的模型设置:
[DataType(DataType.Date)]
public DateTime? Birthday { get; set; }
仍然是文本中的文本文本框显示:
“8/21/2010 12:00:00 AM”
我希望文本框中的文本显示为:
“8/21/2010”
I已经尝试过以下所有内容:
@inherits System.Web.Mvc.WebViewPage<System.DateTime>
但不会让我这样做,因为我@使用模型
I am trying to stop the Time from showing up in a Birthday Text Field that uses DateTime
My Code: (I'm using the Jquery DatePicker)
<label for="birthday">Birthday:</label>
@Html.TextBox("birthday", (Model.Birthday.ToString()), new { @class = "datePicker" })
The Javascript:
$(document).ready(function () {
$('.datePicker').datepicker({ showOn: 'both', buttonImage: "/content/images/calendar-red.gif" });
});
I have the Model setup for the date:
[DataType(DataType.Date)]
public DateTime? Birthday { get; set; }
Still the Text in the Text box displays:
"8/21/2010 12:00:00 AM"
I want Text in the Textbox to diplay as just:
"8/21/2010"
I have tried everything from:
@inherits System.Web.Mvc.WebViewPage<System.DateTime>
but wont let me do that since I am @using a model
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我将在视图模型上使用编辑器模板和数据注释来指定格式,使视图更加清晰:
然后在视图中:
然后调整选择器,因为文本框将不再具有
datePicker
班级:I would use an editor template and data annotations on the view model to specify formatting which makes the views much cleaner:
and then inside your view:
and then adapt the selector because the textbox will no longer have the
datePicker
class:尝试更改为:
@Html.TextBox("birthday", (Model.Birthday.Value.ToShortDateString()), new { @class = "datePicker" })
因为您的 DateTime 是
Nullable
您需要执行.Value
才能访问DateTime
方法。当在
DateTime
上调用ToString
时,它会给你日期 + 时间。因此,您不想调用ToString
,而是使用ToString
带格式 或ToShortDateString
。MSDN 对 ToShortDateString 有以下说法
Try changing to this:
@Html.TextBox("birthday", (Model.Birthday.Value.ToShortDateString()), new { @class = "datePicker" })
Since your DateTime is
Nullable
you need to do.Value
before you can access theDateTime
methods.When calling
ToString
on aDateTime
it will give you Date + Time. So Instead of callingToString
you want to use eitherToString
with formatting orToShortDateString
.MSDN has the following to say about ToShortDateString
我实际上在使用 MVC 3 的 Razor 中遇到了这个问题。我最终不得不在 Shared/EditorTemplates 中创建一个 DateTime 部分类来获取我需要的属性。
以前我的观点是:
@Html.TextBoxWithAttributesFor(m => m.To, new { @class = "text-box", @readonly = "readonly" })
但这保留了给了我时间戳。即使我尝试了 .Value.DateTime 或任何其他解决方法。但我当前的工作解决方案是:view:
partial class:
我使用此页面作为第二个来源:
http:// buildstarted.com/2010/09/10/overriding-displayfor-and-editorfor-to-create-custom-outputs-for-mvc/
I was actually running into this problem with Razor using MVC 3. I ended up having to create a DateTime Partial Class in Shared/EditorTemplates to get the attributes I needed.
Previously my view was:
@Html.TextBoxWithAttributesFor(m => m.To, new { @class = "text-box", @readonly = "readonly" })
but this kept given me the TimeStamp. Even when I tried .Value.DateTime or any other work around. But my current, working solution is:view:
partial class:
I used this page as a second source:
http://buildstarted.com/2010/09/10/overriding-displayfor-and-editorfor-to-create-custom-outputs-for-mvc/
如果您有“Model.Birthday.ToString()”,则需要将其替换为“Model.Birthday.ToShortDateString()”
Where you have "Model.Birthday.ToString()" you need to replace it with "Model.Birthday.ToShortDateString()"