Convert Date To ToShortDateString In Razor

2019-02-12 08:59发布

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.

标签: razor
1条回答
相关推荐>>
2楼-- · 2019-02-12 09:29

Use the [DisplayFormat] attribute on your view model:

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

    ...

}

and then in your view simply:

@model MyViewModel
...
@Html.EditorFor(model => model.StartDate)
查看更多
登录 后发表回答