在 Razor 中将日期转换为 ToShortDateString [英] Convert Date To ToShortDateString In Razor

查看:56
本文介绍了在 Razor 中将日期转换为 ToShortDateString的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个必须在编辑视图中显示的日期时间字段

i have a DateTime Field that must be shown in Edit view

@Html.EditorFor( model => model.StartDate )

我怎么能用这样的东西;

how can i use somthing like this ;

@Html.EditorFor( model => model.StartDate.ToShortDateString() )

或用于更改视图的日期时间的任何自定义函数.

or any custom function for changing the DateTime for view.

推荐答案

使用 [DisplayFormat] 视图模型上的属性:

Use the [DisplayFormat] attribute on your view model:

public class MyViewModel
{
    [DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
    public DateTime StartDate { get; set; }

    ...

}

然后在您看来很简单:

@model MyViewModel
...
@Html.EditorFor(model => model.StartDate)

这篇关于在 Razor 中将日期转换为 ToShortDateString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆