Recovery datetime value in the edit view - asp.net-mvc

When i click in the edit view the datetime lost the value.
enter image description here
<div class="form-group">
#Html.LabelFor(model => model.DataNascimento, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.DataNascimento, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.DataNascimento, "", new { #class = "text-danger" })
</div>
</div>
I would like, when i click in the edit view the value of DataNascimento recive the value from the database.
I hope you guys can help me.

You must force the control to display to dd/mm/yyyy formatted date. to do this you need to format the date.decorate the date property in model as below
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
public System.DateTime DataNascimento{ get; set; }
If you are using MVC5,this should work
#Html.EditorFor(model => model.DataNascimento, new {htmlAttributes = new {#Value = #Model.DataNascimento.ToString("dd-MM-yyyy") } })

Related

DataAnnotation and value by default

I have a property in view model:
[Display(Name = "Date")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
public System.DateTime DateTime { get; set; }
and view:
<div class="form-group">
#Html.LabelFor(model => model.DateTime, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.DateTime, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.DateTime, "", new { #class = "text-danger" })
</div>
</div>
and page shows default value as 01/01/0001 on form:
I want have not any default value (user should specify correct value and can't send form without it). How to remove default value?
You have to make public System.DateTime DateTime { get; set; } a nullable DateTime.

How to change date in editor in MVC?

I have in model DateTime
public System.DateTime date_in { get; set; }
And this is its View:
<div class="form-group">
#Html.LabelFor(model => model.date_in, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.date_in, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.date_in, "", new { #class = "text-danger" })
</div>
</div>
when I just click the EditorFor() to change the date, It writes:
The field date_in must be a date.
all formats I tried to insert (with time and without and change the places of month and day) were wrong.
The individual way I
succeeded is when month value and day value smaller than 13
I have tried answers people answered on similar questions
like add globalization tag and custom binder
but nothing helped me.

I can not save date in MVC

I've this model which has a couple of columns and one of them is date, but on post I get nothing of date, only in case if I give it the value of DateTime.Now then it will save the current date but If had selected some other date in the datepicker it will not save that as it should, so How can I pass the date to the view model on post, I can pass all the date from dropdown and textboxes but date is no where to found. Here is the code
My view model
[Display(Name = "Requested Date")]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
[DataType(DataType.Date)]
public DateTime? RequestDate { get; set; }
[Display(Name = "Job Number")]
public string JobNo { get; set; }
Here is the Post Create method code
if (ModelState.IsValid)
{
var model = new PECEquipmentRequest()
{
ProjectId = pecEquipmentRequest.ProjectId,
JobNo = pecEquipmentRequest.JobNo,
RequestDate = pecEquipmentRequest.RequestDate
};
db.PECEquipmentRequests.Add(model);
db.SaveChanges();
}
and here is the my razor syntax
<div class="form-group">
#Html.LabelFor(model => model.RequestDate, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.RequestDate, new { htmlAttributes = new { #Value = DateTime.Now,#class = "form-control datepicker" } })
#Html.ValidationMessageFor(model => model.RequestDate, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.JobNo, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.JobNo, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.JobNo, "", new { #class = "text-danger" })
</div>
</div>
and here is the kendo date picker that I'm using
$(document).ready(function () {
$(".datepicker").kendoDatePicker();
});
You can parse the date to the required format in the server side.
DateTime.ParseExact(str, "yyyyMMddHHmmss", CultureInfo.InvariantCulture);

Asp.net mvc 5 cross browser date picker and time picker

I programming in mvc5 and i have problem.
My model:
[DataType(DataType.Date)]
[Required(ErrorMessage = "Required")]
public System.DateTime Date { get; set; }
[DataType(DataType.Time)]
[Required(ErrorMessage = "Required")]
public System.DateTime Time { get; set; }
View:
<div class="form-group">
#Html.LabelFor(model => model.Date, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Date, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Date, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Time, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Time, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Time, "", new { #class = "text-danger" })
</div>
</div>
When I use Google chrome date picker and time picker displays correctly, but when I use mozilla firefox this is only a textbox.
How i can to generalize it? I want always to have date picker and time picker.
The default editor template uses the html input type 'date', which is probably not supported in firefox. We created a custom editor template which falls back to jquery date picker (choose whatever js date picker, there are lots) if the browser does not support the date input type.
You can test this feature using modernizr if you want.

Displaying [Date] Value in EditorFor: No overload method for 'ToString' takes 1 argument?

In my INV_Assets Model I have the following Date fields defined:
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)]
public DateTime? acquired_date { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)]
public DateTime? disposed_date { get; set; }
[Required]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)]
public DateTime created_date { get; set; }
Then on my Edit() view I have the following form-group's defined for each of these Date values:
<div class="form-group">
#*#Html.LabelFor(model => model.acquired_date, htmlAttributes: new { #class = "control-label col-md-2" })*#
<span class="control-label col-md-2">Acquired Date:</span>
<div class="col-md-10">
#Html.EditorFor(model => model.acquired_date, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.acquired_date, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#*#Html.LabelFor(model => model.disposed_date, htmlAttributes: new { #class = "control-label col-md-2" })*#
<span class="control-label col-md-2">Disposed Date:</span>
<div class="col-md-10">
#Html.EditorFor(model => model.disposed_date, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.disposed_date, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#*#Html.LabelFor(model => model.created_date, htmlAttributes: new { #class = "control-label col-md-2" })*#
<span class="control-label col-md-2">Created Date:</span>
<div class="col-md-10">
#Html.EditorFor(model => model.created_date, new { htmlAttributes = new { #class = "form-control", #Value = Model.created_date.ToString("yyyy-MM-dd") } })
#Html.ValidationMessageFor(model => model.created_date, "", new { #class = "text-danger" })
</div>
</div>
Now then, when created_date has a value, I can open the form and the EditorFor will properly display the value. However, the EditorFor()'s for acquired_date and disposed_date always show up as mm/dd/yyyy even if there is a value saved in the Database -- if the form is saved while the editor still shows mm/dd/yyyy the value in database changes to null.
When I try to specify acquired_date and disposed_date with (Ex.) #Value = Model.disposed_date.ToString("yyyy-MM-dd") for an htmlAttribute, both Model.acquired_date.ToString("yyyy-MM-dd") and Model.disposed_date.ToString("yyyy-MM-dd") flag as No overload method for 'ToString' takes 1 argument...?
Can anyone see what I am doing wrong?
Ideally I want to save the entered values with whatever Date the user specifies in the EditorFor and with the current Time at the moment of save. I then would like to display the value in format mm/dd/yyyy within the EditorFor if the user is viewing the record.
You've got a few things going here. First, I'd assume that EditorFor is generating an input with type of date. For the HTML5 date input type, the provided value must be in ISO format (YYYY-MM-DD). If it's not in this format, then it's treated as if you provided no value. On post, this is then saved to your database, resulting in the null values. Long and short, you need to change your display format to:
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyInEditMode = true)]
This is where view models come in handy, as you can have this set on the a class used for editing the object and your true desired display format set on a class used to view the object.
Second, your date properties are nullables. So you can't call ToString with a format directly off them. Instead, you have to do something like:
#Model.disposed_date.Value.ToString("MM/dd/yyyy")
However, that will fail if the value is null, so you always need to make sure you have a value first by either wrapping it in an if block:
#if (Model.disposed_date.HasValue)
{
...
}
Or using a ternary:
#(Model.disposed_date.HasValue ? Model.disposed_date.Value.ToString("MM/dd/yyyy") : string.Empty)
The format needs to be [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)] (ISO format)

Resources