DisplayTemplate在ViewBag对象 [英] DisplayTemplate for object in the ViewBag

查看:108
本文介绍了DisplayTemplate在ViewBag对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要显示存储在ViewBag日期和我需要使用DisplayTemplate此。现在,如果这是在模型的日期我会用DisplayFor帮手,但我怎么能做到这一点的ViewBag?

I need to display a date stored in the ViewBag and i need to use DisplayTemplate for this. Now if it was a date in the model i would use DisplayFor helper, but how can i do it for the ViewBag?

我有什么如下:

视图\\共享\\ DateTime.cshtml

@model DateTime
@Html.Display("",Model.ToString("dd-MMM-yyyy"))

Index.cshtml

@((DateTime)ViewBag.Date)

控制器

ViewBag.Date = DateTime.Now

我应该在视图更改,以便有ViewBag.Date显示在什么DD-MMM-yyyy格式?

What should i change in the view in order to have ViewBag.Date display in "dd-MMM-yyyy format?

我知道我可以做到这一点:

I know i can do it by:

@(((DateTime)ViewBag.Date).ToString("dd-MMM-yyyy"))

但在这种情况下,我有我想要显示来自ViewBag日期每次这样做,如果我想改变的格式是什么?我将不得不做出许多变化。

But in this case, i have to do this for each time i want to display a date from ViewBag, and what if i want to change the format? I will have to make many changes.

推荐答案

要更改日期格式为每个项目

to change date format for each item

@foreach (var item in ViewBag.Balance)
{
@Convert.ToDateTime(item.StartDate).ToString("dd/MM/yyyy")
}

探索更多检查,说明 ToShortDateString() ,希望可以帮助别人。

to explore more check this, explains ToShortDateString(), hope helps someone.

这篇关于DisplayTemplate在ViewBag对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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