Geeks With Blogs
Etienne Giust's .NET notes Some .NET tips and tricks for me to remember. That might help you too

I use MVC4 with razor.

I ran into the problem of formating correctly a DateTime property of my model that I render in the view using a TextBoxFor MVC helper. The default formatting displays the full date with hours, minutes and seconds :

 

@Html.TextBoxFor(m => m.BeginDate)

 

displays “27/04/2012 00:00:00” in the textbox. I only needed the first part so I wanted to use the ‘d’ format.

I then tried to use DataAnnotations to impose a format to the property to display:

 

[DisplayFormat(DataFormatString = "{0:d}")]
        public DateTime BeginDate { get; set; }

 

Still, the generated textbox has the full date displayed : “27/04/2012 00:00:00”

 

There are actually 2 solutions to that :

 

 

1- Use DataAnnotations + EditorFor

 

The dataannotation DisplayFormat is actually NOT taken into account when the data is rendered with @Html.TextBoxFor helper. It applies only to the EditorFor helper.

So, keeping your dataannotation and replacing your helper by this shoud work :

 

@Html.EditorFor(m => m.BeginDate)

 

 

2 -Use TextBoxFor and alter the @Value parameter

 

If you really want your TextBoxFor, there is also a solution which I find less desirable since it tinkles with the default behavior of TextBoxFor.

You can remove the DisplayFormat data annotation and use this helper :

 

@Html.TextBoxFor(m => m.BeginDate, new { @Value = Model.BeginDate.ToString("d") })
Posted on Friday, April 27, 2012 12:58 AM | Back to top


Comments on this post: Apply a DateTime Format in an Asp.Net MVC TextBoxFor

# re: Apply a DateTime Format in an Asp.Net MVC TextBoxFor
Requesting Gravatar...
Very nice trick. It saves my time. Thankyou
Left by Naveed Ahmad on Dec 17, 2012 9:53 PM

# re: Apply a DateTime Format in an Asp.Net MVC TextBoxFor
Requesting Gravatar...
Thanks for the post!! It saved my time too.
Left by Neha on Feb 05, 2013 8:41 PM

# re: Apply a DateTime Format in an Asp.Net MVC TextBoxFor
Requesting Gravatar...
thank you so much
Left by ufukates on Feb 21, 2013 2:05 PM

# re: Apply a DateTime Format in an Asp.Net MVC TextBoxFor
Requesting Gravatar...
nice post... clear and to the point... was helpful, thanks. Remember to add ApplyFormatInEditMode = true to the DataAnnotation.
Left by cvc on Feb 25, 2013 3:42 PM

# re: Apply a DateTime Format in an Asp.Net MVC TextBoxFor
Requesting Gravatar...
Very helpful. Thanks a lot!
Left by Marcaldo on Mar 07, 2013 1:52 AM

# re: Apply a DateTime Format in an Asp.Net MVC TextBoxFor
Requesting Gravatar...
Thank you very much, It saves my time and my life too.
Left by Emdii on May 30, 2013 8:10 PM

# re: Apply a DateTime Format in an Asp.Net MVC TextBoxFor
Requesting Gravatar...
thanks, you helped me much!

(www.work.az)
Left by Ceyhun on Jun 06, 2013 1:24 AM

# re: Apply a DateTime Format in an Asp.Net MVC TextBoxFor
Requesting Gravatar...
Thanks. Its very helpful.
Helped me to fix the issue.
Left by Agatheeswaran on Jul 18, 2013 11:54 PM

# re: Apply a DateTime Format in an Asp.Net MVC TextBoxFor
Requesting Gravatar...
it's very funny
Left by Rajkiran swain on Aug 18, 2013 9:14 PM

# re: Apply a DateTime Format in an Asp.Net MVC TextBoxFor
Requesting Gravatar...
Hi, nice solution
thanks
Left by Om Prakash Bishnoi on Sep 07, 2013 9:05 AM

# re: Apply a DateTime Format in an Asp.Net MVC TextBoxFor
Requesting Gravatar...
Perhaps an extension method could be created:

public static MvcHtmlString DateTimeFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression)
{
var compilationResult = expression.Compile();
TValue dateValue = compilationResult((TModel)html.ViewDataContainer.ViewData.Model);
var body = (MemberExpression)expression.Body;
return html.TextBox(body.Member.Name, (Convert.ToDateTime(dateValue)).ToCustomDateFormat(), new { id = body.Member.Name, datepicker = true });
}

the method ToCustomDateFormat could be an extension method for dateTime types which returns string value in desired format.
Left by Abhishek Bussa on Sep 10, 2013 7:26 AM

# re: Apply a DateTime Format in an Asp.Net MVC TextBoxFor
Requesting Gravatar...
It was so useful for me

Thanks.
Left by Isac Ribeiro on Sep 13, 2013 9:46 AM

# re: Apply a DateTime Format in an Asp.Net MVC TextBoxFor
Requesting Gravatar...
Thanks from the depth f ma heart... saved my hours..!!
Left by Unais on Nov 11, 2013 3:53 AM

# Convert DateTime To date In mvc 4
Requesting Gravatar...
Hello I have Convert datetime To date In mvc 4
Left by Shah Ritesh on Mar 20, 2014 5:31 PM

# re: Apply a DateTime Format in an Asp.Net MVC TextBoxFor
Requesting Gravatar...
Did not work for me date still persists in showing both date and time. (All my editable fields use editorfor).
Left by Edward on May 15, 2014 8:18 AM

# re: Apply a DateTime Format in an Asp.Net MVC TextBoxFor
Requesting Gravatar...
it is working but after this datepicker control is not working for me.
Left by sams on May 29, 2014 6:31 PM

# re: Apply a DateTime Format in an Asp.Net MVC TextBoxFor
Requesting Gravatar...
Thnaks ! helped me too..@Sams use code like below datepicker will work: @Html.TextBoxFor(model=>model.EndDate,new { @Value = Model.EndDate != null ? Convert.ToDateTime(Model.EndDate).ToString("d"): "" })
Left by Rajnish on Dec 09, 2014 9:56 PM

# re: Apply a DateTime Format in an Asp.Net MVC TextBoxFor
Requesting Gravatar...
Hi, This is very helpful thanks for sharing. How come you people got dd/mm/yyyy format? I am failing to do that. Could you please help me.
Left by Satya on Dec 23, 2014 7:19 PM

# re: Apply a DateTime Format in an Asp.Net MVC TextBoxFor
Requesting Gravatar...
Thanks, this was very helpful.
Left by Danielle on Jan 30, 2015 9:50 AM

# re: Apply a DateTime Format in an Asp.Net MVC TextBoxFor
Requesting Gravatar...
Not working in my case. It says Object reference not set to an instance of an object.
Have tried a lot many things but isn't getting a solution.
Left by Sneha on Feb 10, 2015 7:35 PM

# re: Apply a DateTime Format in an Asp.Net MVC TextBoxFor
Requesting Gravatar...
Thanks. It was the only solution that worked for me.
Left by Ben Junior on Mar 12, 2015 3:44 AM

Your comment:
 (will show your gravatar)


Copyright © Etienne Giust | Powered by: GeeksWithBlogs.net | Join free