I want to format the DateTime displayed in TextBoxes without the time and to this format: 01 Jan, 2011 instead of the default 01/01/2011 for both display and edit scenarios.
I am already using the following template for DateTime in order to use datepicker. Can I somehow include formatting here?
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<System.DateTime?>" %>
<%: Html.TextBox("",ViewData.TemplateInfo.FormattedModelValue, new { @class = "text-box single-line" }) %>
Thanks in advance.
is
ViewData.TemplateInfo.FormattedModelValuea datetime object?to get the format you want out of a DateTime object use the following:
this should work
The reason you are getting an error when you call
Model.ToString("dd MMM, yyyy")is because it’s a Nullable object, and it’s ToString() method doesn’t accept any parameters. The value of the model is a DateTime object, which does accept parameters.