Problems with DateTime, it appears with 00:00:00

I have my database which has a DateTime type for Birthdate value when I register the user.

When I receive my data back, to edit the register form, I have my Razor like this:

@Html.TextBoxFor(model => model.Birthdate)

The date shows like this: 28/05/1983 00:00:00

I want only the birthdate, obviously. In my Controller, I have this:

User userset = db.User.Find(id);
return View(userset);

Very simple... Could anyone help me to solve that?


You can do something like this using Html.TextBox instead of using Html.TextBoxFor.

@Html.TextBox("Birth date", String.Format("{0:dd/MM/yyyy}", Model.Birthdate))

Set the DisplayFormat data annotation above the property in your model.

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


Then instead of using @Html.TextBoxFor(..) use @Html.EditorFor(...).

See the DisplayFormatAttribute MSDN page for more details.

If you have generated your data model using EF you can simply create a meta class for your class to apply data annotations. For example, if my db file is called MyDB.edmx, create a buddy class file called MyDB.cs. Then inside there, I would extend the User class by attaching a metadata class to it and specifying the data annotation in the metaclass:

public partial class User{ }

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

See Scott Gu's post on validation, mainly the section 'But what if we are using a graphical tool for our ORM mappings?'.

Need Your Help

How do I send the result set from a query as an attachment in an email using SSIS?

sql-server ssis

I have a result set from an Execute SQL Task query saved on a System.Object variable and i would like to send this results using a Send Mail Task using the following expression on the MessageSource...

Emacs regexp groups in regex-replace

regex emacs elisp

I have a bunch of C macros in files, stuff like NEXT( pL ) which is expanded to ( ( pL ) -> next )