MVC5 date problem

lg127

Well-Known Member
Joined
Nov 8, 2010
Messages
273
Hi there

I am fairly new at ASP.Net MVC and have a problem i can't seem to figure out.
I have created a web application using VS2015 and C# and the MVC framework. The data base is SQL Server 2014 and I have made use of scaffolding.
I also made use of data annotations to format my date and has apart from other fields 3 date fields which has been annotated as in my model:
[Required(ErrorMessage = "This field is Required!")]
[DataType(DataType.Date)]


in my view I have:
<div class="form-group">
@Html.LabelFor(model => model.DATEREQUIREDBY, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.DATEREQUIREDBY, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.DATEREQUIREDBY, "", new { @class = "text-danger" })
</div>
</div>


I have also set a datepicker with the date formatted as a shortdatestring.

The problem I have is that the date will display correctly in the list of records but when I edit a record in the list (with a date) the date is displayed as mm/dd/yyyy in the form instead of the actual date (21/09/2016) of that record I am trying to edit.

Thanks in advance..
 

Solitude

Executive Member
Joined
Jul 23, 2008
Messages
7,312
Try something like this:

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
public DateTime DATEREQUIREDBY{ get; set; }

*edit*

Scratch that!

Use this:
@Html.TextBoxFor(m => m.EndDate, "{0:dd/MM/yyyy}", new { @class="form-control" })
 
Last edited:

lg127

Well-Known Member
Joined
Nov 8, 2010
Messages
273
Try something like this:

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
public DateTime DATEREQUIREDBY{ get; set; }

*edit*

Scratch that!

Use this:
@Html.TextBoxFor(m => m.EndDate, "{0:dd/MM/yyyy}")


Thank you, I will try that when I get home.
 

halfmoonforever

Expert Member
Joined
Feb 1, 2016
Messages
1,196
Format your date as dd-MMM-yyyy and never worry about SQL Server vs MVC issues again. 23-Sep-2016 is universal and won't break even if you run it on a Russian server (regional settings, yay)
 

lg127

Well-Known Member
Joined
Nov 8, 2010
Messages
273
It's a pleasure. I edited it again to include your form-control class. It should work correctly for you then.

Unfortunately it did not work. Same error. The records date field on the EDIT form is displayed as "dd/MM/yyyy" iso "18/09/2016".
 

lg127

Well-Known Member
Joined
Nov 8, 2010
Messages
273
Format your date as dd-MMM-yyyy and never worry about SQL Server vs MVC issues again. 23-Sep-2016 is universal and won't break even if you run it on a Russian server (regional settings, yay)

No, it is not the formatting that is the problem. The problem is the date field on the EDIT form is displayed as (in your case's example) as dd-MMM-yyyy" iso "18-Sep-2016". The date displays correctly on the INDEX LIST form.
 

halfmoonforever

Expert Member
Joined
Feb 1, 2016
Messages
1,196
No, it is not the formatting that is the problem. The problem is the date field on the EDIT form is displayed as (in your case's example) as dd-MMM-yyyy" iso "18-Sep-2016". The date displays correctly on the INDEX LIST form.

Then fix it?
 

Solitude

Executive Member
Joined
Jul 23, 2008
Messages
7,312
Unfortunately it did not work. Same error. The records date field on the EDIT form is displayed as "dd/MM/yyyy" iso "18/09/2016".

I thought that's how you wanted to display it. I'm not entirely sure that I understand your problem.
 

Solitude

Executive Member
Joined
Jul 23, 2008
Messages
7,312
The problem I have is that the date will display correctly in the list of records but when I edit a record in the list (with a date) the date is displayed as mm/dd/yyyy in the form instead of the actual date (21/09/2016) of that record I am trying to edit.

Thanks in advance..

Unfortunately it did not work. Same error. The records date field on the EDIT form is displayed as "dd/MM/yyyy" iso "18/09/2016".

So if it's showing as dd/MM/yyyy now then isn't that what you wanted? Because you were saying that it was showing as mm/dd/yyyy originally.

If not, then what do you actually want to display?
 

lg127

Well-Known Member
Joined
Nov 8, 2010
Messages
273
So if it's showing as dd/MM/yyyy now then isn't that what you wanted? Because you were saying that it was showing as mm/dd/yyyy originally.

If not, then what do you actually want to display?

Apologies for the late reply.

I want is to display the ACTUAL date in the record when I edit the record. Eg. I want it to display 19/09/2016 and not dd/MM/yyyy.
 

lg127

Well-Known Member
Joined
Nov 8, 2010
Messages
273
When I create a new record then displaying dd/MM/yyyy in the field is ok to show the user the format. But when an existing record with a value in the date filed is edited I want it to display the actual date value. Hope it makes sense .
 

Solitude

Executive Member
Joined
Jul 23, 2008
Messages
7,312
Hmmm can you paste the code for your model?

Also a screenshot of what you are currently seeing?
 

skimread

Honorary Master
Joined
Oct 18, 2010
Messages
12,419
Try
Code:
@Html.TextBoxFor(m=>m.DATEREQUIREDBY, "{0:dd/MM/yyyy}", new { @class = "form-control" })
 

lg127

Well-Known Member
Joined
Nov 8, 2010
Messages
273
Hmmm can you paste the code for your model?

Also a screenshot of what you are currently seeing?

Model:
--------
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated from a template.
//
// Manual changes to this file may cause unexpected behavior in your application.
// Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace IRMS.Models
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

public partial class Change
{
public int CHANGEID { get; set; }
public string TITLE { get; set; }
public string DESCRIPTION { get; set; }

[DataType(DataType.Date)]
public System.DateTime DATEREQUIREDBY { get; set; }
public int REQUESTEDBY { get; set; }
public int FORDEPARTMENT { get; set; }

[DataType(DataType.Date)]
public System.DateTime LOGGEDDATE { get; set; }
public int LOGGEDBY { get; set; }
public int TYPE { get; set; }
public int PRIORITY { get; set; }
public int CLASSIFICATION { get; set; }
public int CATEGORY { get; set; }
public int ASSIGNEDGROUP { get; set; }
public Nullable<int> ASSIGNEDPERSON { get; set; }
public Nullable<decimal> ESTIMATEDHOURS { get; set; }

[DataType(DataType.Date)]
public Nullable<System.DateTime> STARTDATE { get; set; }

[DataType(DataType.Date)]
public Nullable<System.DateTime> ENDDATE { get; set; }
public Nullable<decimal> ACTUALHOURS { get; set; }
public int STATUS { get; set; }
public int STATUSPERSON { get; set; }
public string STATUSREASON { get; set; }

public virtual CATEGORy CATEGORy1 { get; set; }
public virtual CLASSIFICATION CLASSIFICATION1 { get; set; }
public virtual CUSTOMER CUSTOMER { get; set; }
public virtual DEPARTMENT DEPARTMENT { get; set; }
public virtual GROUPSTAFF GROUPSTAFF { get; set; }
public virtual PRIORITy PRIORITy1 { get; set; }
public virtual Status Status1 { get; set; }
public virtual SUPPORTSTAFF SUPPORTSTAFF { get; set; }
public virtual SUPPORTSTAFF SUPPORTSTAFF1 { get; set; }
public virtual SUPPORTSTAFF SUPPORTSTAFF2 { get; set; }
public virtual TYPE TYPE1 { get; set; }
}
}
--------

Code in the EDIT view:
...
...
<div class="form-group">
@Html.LabelFor(model => model.DATEREQUIREDBY, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.DATEREQUIREDBY, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.DATEREQUIREDBY, "", new { @class = "text-danger" })
</div>
</div>
...
...
<div class="form-group">
@Html.LabelFor(model => model.LOGGEDDATE, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.LOGGEDDATE, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.LOGGEDDATE, "", new { @class = "text-danger" })
</div>
</div>
...
...
----------

Details & Edit Screens:
https://www.dropbox.com/s/uis79alzhwx7dqq/img01.png?dl=0

Hope this is enough info.
 

lg127

Well-Known Member
Joined
Nov 8, 2010
Messages
273
OP is this for a campus project or are you just learning?

A campus project but each student had to come up with his own unique project and his choice of programming language.
I chose ASP.NET (MVC) as I did web programming many years ago (pre-ASP.NET) and it is not as easy to learn as I thought it would be!:eek:
 

lg127

Well-Known Member
Joined
Nov 8, 2010
Messages
273
Try
Code:
@Html.TextBoxFor(m=>m.DATEREQUIREDBY, "{0:dd/MM/yyyy}", new { @class = "form-control" })

See my post at 24-09-2016 02:17 PM for a better explanation of my problem. The date formatting is not the issue now, it is the fact that the date on a record I want to edit is not displayed but the string "mm/dd/yyyy" :confused:
 

skimread

Honorary Master
Joined
Oct 18, 2010
Messages
12,419
See my post at 24-09-2016 02:17 PM for a better explanation of my problem. The date formatting is not the issue now, it is the fact that the date on a record I want to edit is not displayed but the string "mm/dd/yyyy" :confused:
You need to pass the Date property from your controller.

In your Edit HTTPGet method in the controller return the model e.g. making sure the property passed is a date

Code:
           var change= await _changeRepo.SelectById(id); //fetch entity from database
           change.DATEREQUIREDBY=DateTime.Now;//hardcode it to test
           return View("~/Views/Change/_Edit.cshtml", change);


cshtml
Code:
     @model IRMS.Models.Change @*on the first line*@
      ....
     @Html.TextBoxFor(model=>model.DATEREQUIREDBY, "{0:dd/MM/yyyy}", new { @class = "form-control" })
 
Last edited:
Top