Edward Joell
asked on
Format Date in View Model DisplayFor and EditorFor
I had a question on Forums.Asp.Net that asked
"The Format date in view Model at http://forums.asp.net/t/1945089.aspx?Format+Date+in+view+model tells how to add an attribute to a date property in the model like below to get it to display as a Date only. However I am wondering if this is done with an Entity Framework Database First Model, will this be maintained after you do an Update From Database?"
The code I am referring to is here:
However the EditorFor's and DisplayFor's did not display the specified format. does anyone see what I am doing incorrectly and provide guidance? It is important to note that both of those fields have JQueryUI datepickers attached so that it is important that it can read the entry as a date.
"The Format date in view Model at http://forums.asp.net/t/1945089.aspx?Format+Date+in+view+model tells how to add an attribute to a date property in the model like below to get it to display as a Date only. However I am wondering if this is done with an Entity Framework Database First Model, will this be maintained after you do an Update From Database?"
The code I am referring to is here:
Display(Name = "Start Date")]
[Required(ErrorMessage = "Please enter a start date")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
public string startDate { get; set; }
I was told to use the Metadata files so eventually I changed the meta data files as belowusing System;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
using System.Linq;
using System.Web.Mvc;
namespace VFS_ProcurementTracking.Models
{
[MetadataType(typeof(prc_RequirementMetaData))]
public partial class prc_Requirement
{
internal sealed class prc_RequirementMetaData
{
private prc_RequirementMetaData()
{
}
}
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
public Nullable<System.DateTime> CustomerRDD { get; set; }
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
public System.DateTime Import_CreateDate { get; set; }
}
}
However the EditorFor's and DisplayFor's did not display the specified format. does anyone see what I am doing incorrectly and provide guidance? It is important to note that both of those fields have JQueryUI datepickers attached so that it is important that it can read the entry as a date.
How are you outputting the value in your view? The strongly typed editors (i.e. TextBoxFor, etc) don't support the DataFormatString, whereas the more general EditorFor does
ASKER
I am using EditorFor and DisplayFor. I am insufficiently experienced to play around with tools other than those two and dropDownlistfor.
Your metadata properties should be inside the metadata class. Like:
namespace VFS_ProcurementTracking.Models
{
[MetadataType(typeof(prc_RequirementMetaData))]
public partial class prc_Requirement
{
internal sealed class prc_RequirementMetaData
{
private prc_RequirementMetaData()
{
}
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
public Nullable<System.DateTime> CustomerRDD { get; set; }
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
public System.DateTime Import_CreateDate { get; set; }
}
}
}
If that still doesn't work, can you post the code for your View too.
ASKER
Maybe my eyes aren't working properly. However, I fail to see how that differs from the code I posted above. Can someone point out what I am missing?
In your original post you have the Metadata properties outside of your metadata class. The revised version moves the closing brace of the metadata class down, so that it incorporates the properties.
So basically the brace at line 20 in your original post is now at line 17 of the revised version.
So basically the brace at line 20 in your original post is now at line 17 of the revised version.
ASKER
Great. i will try that.
ASKER
I set up the meta data like this
It made no difference. Both date and time are shown in EditorFor and DisplayFor.
Same fields different issue. The one field is nullable but the other is not. So when an empty "Create" view is shown, the editorfor is showing 01/01/0001 12:00:00 AM. I would like that to show the current date and only the date by default. How can I do that.
using System;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
using System.Linq;
using System.Web.Mvc;
namespace VFS_ProcurementTracking.Models
{
[MetadataType(typeof(prc_RequirementMetaData))]
public partial class prc_Requirement
{
internal sealed class prc_RequirementMetaData
{
private prc_RequirementMetaData()
{
}
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
public Nullable<System.DateTime> CustomerRDD { get; set; }
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
public System.DateTime Import_CreateDate { get; set; }
}
}
}
It made no difference. Both date and time are shown in EditorFor and DisplayFor.
Same fields different issue. The one field is nullable but the other is not. So when an empty "Create" view is shown, the editorfor is showing 01/01/0001 12:00:00 AM. I would like that to show the current date and only the date by default. How can I do that.
Is your prc_Requirement class in the same namespace as the underlying Entity Framework class?
The code you have is technically correct for what you are trying to do, so the issue may be that it is treating your class as separate from the underlying entitiy.
The code you have is technically correct for what you are trying to do, so the issue may be that it is treating your class as separate from the underlying entitiy.
ASKER
yes
ASKER
As state above the the prc_Requirement class is in the same namespace. So what would be treating my class as separate from the underlying entity, and why?
ASKER
Here is a thought
The model being displayed in the view contains a property of type "prc_Requirement" above. Maybe, it is not enough to set the DisplayFormat in metadata for the "prc_Requirement model" but in metadata for the "RequirementViewShowingMod el" as well?
The model being displayed in the view contains a property of type "prc_Requirement" above. Maybe, it is not enough to set the DisplayFormat in metadata for the "prc_Requirement model" but in metadata for the "RequirementViewShowingMod
ASKER
But the question is since the fields CustomerRDD and Import_ExportDate are in the Requirements Model I do I refer to them in the Metadata file? Just setting them with there own names did not work as the view could not find the fields for which the metadata was created. Referring to them as Requirement.CustomerRDD and Requirement.Import_ExportD ate did not work because of compile time errors.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I've requested that this question be closed as follows:
Accepted answer: 0 points for Joeller's comment #a40068533
for the following reason:
This works excellently. CUDOS to Rachel Appel
Accepted answer: 0 points for Joeller's comment #a40068533
for the following reason:
This works excellently. CUDOS to Rachel Appel
ASKER
Need to Undo this as solved because the templates do not work on the Create view because at that time the Model's DateTime property is null and the template caused an error "The model item passed into the dictionary is null, but this dictionary requires a non-null model item of type 'System.DateTime'." So I need a way for this to work in the create View
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Kudos to StackOverflow's Fabian, and Rachel Appel