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

Detect common words in strings Ruby

ruby string

How can I detect a common substring between two or more sentences in Ruby.

bash script to loop and rename file(s)


I use csplit to write separate smaller files from a single larger file

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.