andy
andy

Reputation: 595

Issue To fetch date format At Edit() In ASP.NET MVC 3

When I edit my one of the form sin ASP.NET MVC 3 in this when I edit a user registration form then I got date in startdate is 21-Mar-12 12:00:00 AM in text box but I need 21-Mar-12.

So how can I format textbox date like that?

Upvotes: 0

Views: 251

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038930

You could decorate the DateTime property on your view model with the [DisplayFormat] attribute which allows you to specify a given format:

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd-MMM-yy}")]
public DateTime SomeDate { get; set; }

and in your strongly typed view use the EditorFor helper to render the corresponding input field:

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

As far as binding the value back to a DateTime field when posting back is concerned you could write a custom model binder that will use this format. I have shown an example here.

Upvotes: 1

Related Questions