开发者

Convert Date To ToShortDateString In Razor

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.


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)


I do it like this

<td>
    @{ 
       var date = item.PostDate.ToShortDateString();
     }
     @Html.DisplayFor(modelItem => date)
</td>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜