0

Hey, This is kind of tough to explain because i don't even know how it is happening. I have a datepicker box which when the page is loaded that date box is set to null. Once the user chooses a date and clicks the submit button - the page is reloading and working as it should fine but a time format of zeros appears with the date like so :

5/11/2011 00:00:00

Is there a way i can get rid of the zeros in my post or get methods or any way possible?

Here is how my code looks like in the aspx page:

Begin Date: <%:Html.EditorFor(b => b.BeginDate)%><%:Html.ValidationMessageFor(b => b.BeginDate)%>

End Date: <%:Html.EditorFor(e => e.EndDate)%><%:Html.ValidationMessageFor(e => e.EndDate)%>

My ViewModel:

public DateTime? BeginDate { get; set; }

public DateTime? EndDate { get; set; }

This is based off of Darin's Answer:

in the ViewModel:

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString ="{0:dd/MM/yyyy}")]
    public DateTime? BeginDate { get; set; }

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
    public DateTime? EndDate { get; set; }

And in my .aspx page :

Begin Date: <%:Html.EditorFor(b => b.BeginDate)%><%:Html.ValidationMessageFor(b => b.BeginDate)%>

End Date: <%:Html.EditorFor(e => e.EndDate)%><%:Html.ValidationMessageFor(e => e.EndDate)%>
Masriyah
  • 2,445
  • 11
  • 49
  • 91
  • check this out; it is in WPF though http://stackoverflow.com/questions/3819832/changing-the-string-format-of-the-wpf-datepicker – Sandeep G B May 11 '11 at 04:51
  • in my viewModel this is how my dates are displayed: public DateTime? BeginDate{get;set;} public DateTime? EndDate{get; set;} my html markup is: Begin Date: <%:Html.EditorFor(b => b.BeginDate)%><%:Html.ValidationMessageFor(b => b.BeginDate)%> End Date: <%:Html.EditorFor(e => e.EndDate)%><%:Html.ValidationMessageFor(e => e.EndDate)%> – Masriyah May 11 '11 at 12:53
  • @Splendor - thanks for the link i will try it out and see if i could apply it. Thanks – Masriyah May 11 '11 at 13:00

2 Answers2

1

You could use the DisplayFormat attribute on your view model:

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

and in your view you would generate the corresponding input field using the EditorFor helper:

@Html.EditorFor(x => x.Date)

Now you could attach a datepicker to the resulting input using the same format and when the form is submitted it will keep the same format for the date field.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • can i still keep my datetime values null-able? like DateTime? Date{get;set} – Masriyah May 11 '11 at 12:45
  • Thanks for the answer but for some reason i am still getting the zeros to show after i click the submit button :( I am not sure what the cause could be. I have the date so that i could be null and as it in as parameters in different places as null could that affect it in any way? Thanks again – Masriyah May 11 '11 at 12:59
  • @Amina, after updating your question and showing how the view model look like I can't see the `BeginDate` and `EndDate` properties decorated with the `[DisplayFormat]` attribute as in my answer. – Darin Dimitrov May 11 '11 at 13:35
  • Sorry i posted the original what i started with - i will go ahead and post edit adding the attributes from your answer :) – Masriyah May 11 '11 at 13:43
0

I was actually able to control it in my report file. in my .rdlc file i created an expression in the box i will show my date to the following:

DateValue(Parameters!BeginDate.Value)

and that eliminated the zeros from appearing in my text box as well.

thanks for everyone's help

Masriyah
  • 2,445
  • 11
  • 49
  • 91