Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 12818
  • Last Modified:

how to format date with Html.TextBoxFor helper ?

how to format date which is displayed using  Html.TextBoxFor helper  class.
0
hariaspind
Asked:
hariaspind
  • 2
  • 2
1 Solution
 
jamesbaileCommented:
There are a number of ways you could do this.

You could create a DateTime editor template, which will mean that all dates are edited in the same way.

Have a look at the following which describes how to implement a jquery date picker.

http://elegantcode.com/2009/11/13/using-the-jquery-ui-datepicker-with-asp-net-mvc-2-templates/ 

0
 
hariaspindAuthor Commented:
I was trying to use the below syntax's.But the formatting is not working .  
 
<%= Html.TextBoxFor(model => model.ExpirationDate, String.Format("{0:MM/dd/yyyy}",
Model.ExpirationDate))%>


0
 
jamesbaileCommented:
The issue is that the String.Format("{0:MM/dd/yyyy}", Model.ExpirationDate) bit above is the TemplateName parameter of the TextBoxFor method. The view engine won't be able to find a template which matches the date.

What you need to do is decorate your model

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
[DisplayName("Date Order Required")]
public DateTime DateRequired { get; set; }

and then use the following in your view

<%= Html.EditorFor(model => model.DateRequired)%>
0
 
hariaspindAuthor Commented:
Thanks
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now