asp.net mvc 3 - remove time from date - asp.net-mvc-3

I am having a model in with a DateTime property:
public DateTime? DateNaissance { get; set; }
and a View with a #Html.TextBoxFor for the property
#Html.TextBoxFor(model => model.DateNaissance)
All I want is to get the date typed, however, when I type in 01/06/2012 as date I am having the "01/06/2012 00:00:00" in the controller
All I want is to get the date, why the time is added ?, how to automatically remove it ?
I tried out without success:
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")]
Thanks

You need to set the ApplyFormatInEditMode property of the DisplayFormatattribute to true and then use EditorFor instead of TextboxFor:
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
#Html.EditorFor(model => model.DateNaissance)
Update
If you need to do this for every date, you can also put a file called DateTime.cshtml in \Views\Shared\EditorTemplates\ with this in it:
#model DateTime?
#Html.TextBox("", (Model.HasValue ? Model.Value.ToString("dd/MM/yyyy") : string.Empty))
This will use "dd/MM/yyyy" for DateTimes when you use EditorFor.

It's worth to say that you will get always the time since your object is of DateTime type. If you want to use only the Date, on your controller you need to get only the Date part with DateNaissance.Date, but again the object will always have the time part on it

Related

facing issue with day in mvc when selecting through datepicker

I am facing DateTime issue in MVC, wherein when I select the date from date picker in the kendo grid in my model flightarrivalmodel i have a property called departure date. every time it will take 1 day less than selected. ex: if I select 3-12-2019 in the model it will be 2-12-2019, can you please tell me the root cause for this the code is very simple.
I have a property with a departure date as DateTime datatype. code as follows
[UIHint("Date")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy-MM-dd}")]
[Display(Name = "Date")]
public DateTime DepartureDate { get; set; }

Telerik asp.net for MVC datepicker format in grid related issue

I have specified date format as dd/mm/yyyy for telerik datepicker. However, it is recognized as mm/dd/yyyy only. For example, if i enter date as 18/10/2012 error on saving record is thrown as date format is not valid as date entered here as 18 is treated as month. I have defined view for date as:
#model DateTime?
#(Html.Telerik().DatePickerFor(m => m)
.Name(ViewData.TemplateInfo.GetFullHtmlFieldName(string.Empty))
.Format("dd/MM/yyyy")
.HtmlAttributes(new { id = ViewData.TemplateInfo.GetFullHtmlFieldName(string.Empty) + "_wrapper" })
.InputHtmlAttributes(new {style="width:160px;"}))
And in model validation for date field is defined as:
[Required(ErrorMessage = "Date Required")]
[DataType(DataType.Date)]
public DateTime? DATE_CALL { get; set; }
Also, in grid has defined column as:
col.Bound(r => r.DATE_CALL).Format("{0:dd/MM/yyyy}").Title("DATE_CALL").Width(170);
So, can't figure out what the error is? Any help will be appreciated.
use the following to set your date format before saving to the database:
DateTime.ParseExact(<yourDate>, "MM/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture);

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?
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:
[MetadataType(typeof(UserMetaData))]
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?'.
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))
DatePicker from jQuery is a better approach to receive date input from user. It is particularly helpful to avoid various date format confusion.
More details on: http://blogs.msdn.com/b/stuartleeks/archive/2011/01/25/asp-net-mvc-3-integrating-with-the-jquery-ui-date-picker-and-adding-a-jquery-validate-date-range-validator.aspx

How can I use DisplayFormat data annotation in WebGrid columns?

I have the following property in my model:
[DisplayFormat(DataFormatString = "{0:d}")]
public DateTime? Date { get; set; }
And I am trying to use the Html.DisplayFor helper to use this specification in a WebGrid column, like so:
Sources.Column("Date", "As Of Date", (item) => Html.DisplayFor(x => item))
When I run this, I get a lot of extra information in the column, and the date comes out as a long format date, instead of the desired short format. The output I get makes me suspect that DisplayFor is looking through each property in the model and printing it, instead of just looking at Date. Why would it do this? Is there something I can do to use DisplayFor in the WebGrid?
When I try to specify item.Date I get the error "An expression tree may not contain a dynamic operation"
Try this code snippet instead of what you have now:
Sources.Column( header: "Date", format: ( item -> { return #Html.Raw(item.date.ToString("d")); }))

Remove default value for non nullable properties when using EditFor [asp.net mvc 3]

How can I remove the default value that is added by default to the textboxes of non nullable properties when using the EditFor helper? I don't want that behavior
EDIT
Sorry I didn't give enough information.
For example if you use Html.EditorFor with a property that is DateTime it will set the textbox value to 1/1/0001 automatically. If you use "DateTime?"(nullable), it won't, it just leaves the textbox empty.
You can use UIHint to do it.
Create a file called ShortDate.cshtml in EditorTemplates
#model DateTime
#{ var value = Model == default(DateTime) ? null : Model.ToShortDateString(); }
#Html.TextBox(string.Empty, value)
Decorate your property with the UIHintAttribute referencing our EditorTemplate. Consider my Order class.
public class Order {
[UIHint("ShortDate")]
public DateTime Date { get; set; }
}
When you use
#Html.EditorFor(x => x.Date)
it should avoid the default value of DateTime
caveat: I just did simple tests, so please take a deep look into it.
hope it helps you
I had to do something like this for my own needs. I used this:
#model DateTime?
#Html.TextBox("", (Model.Value != default(DateTime) ? Model.Value.ToShortDateString() : string.Empty))
and it worked pretty nicely for my DateTime values. Ones that didn't have the default value are blank and the ones that have some other DateTime value show the ShortDateString representation of the object.

Resources