I need to display the date stored in the ViewBag, and for this I need to use DisplayTemplate. Now, if it was a date in the model, I would use the DisplayFor helper, but how can I do this for the ViewBag?
I have the following:
Views \ Shared \ DateTime.cshtml
@model DateTime @Html.Display("",Model.ToString("dd-MMM-yyyy"))
Index.cshtml
@((DateTime)ViewBag.Date)
controller
ViewBag.Date = DateTime.Now
What should I change in the view to display the ViewBag.Date in dd-MMM-yyyy format?
I know I can do this:
@(((DateTime)ViewBag.Date).ToString("dd-MMM-yyyy"))
But in this case, I have to do this every time I want to display the date from the ViewBag, and what if I want to change the format? I will need to make a lot of changes.
source share