Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8796811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:42:28+00:00 2026-06-13T23:42:28+00:00

I use MVC 3 in C#, I have a class with this Attribute [DisplayFormat(DataFormatString

  • 0

I use MVC 3 in C#, I have a class with this Attribute

[DisplayFormat(DataFormatString = "{0:dd MMM yyyy}", ApplyFormatInEditMode = true)]

I would like to enforce Validation when a User is in EDIT MODE

Data in database is stored with type datetime formatted like this

  6/15/2009 1:45:30 PM

I receive this error

Error String is not in correct format

I believe the problem is in

DataFormatString = "{0:dd MMM yyyy}"

any idea how to fix it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T23:42:29+00:00Added an answer on June 13, 2026 at 11:42 pm

    The DisplayFormat attribute is literally only for displaying the value. If you set the ApplyFormatInEditMode all it will do is also apply the format to the contents of your data when displayed in a textbox (intended for Editing). It’s got nothing to do with validation.

    If you want to validate the input using the format you’ve specified you’ll likely have to create your own ValidationAttribute, and use DateTime.ParseExact() to attempt to ensure it meets the format you’re expecting. The only draw back is it will not have an accompanying client side validation logic unless you write it.

    I have not tested this thuroughly, but it should give you a start.

    public class DateTimeFormatAttribute : ValidationAttribute
    {
       public int Format { get; set; }
    
       public override bool IsValid(object value)
       {
            if (value == null)
                return true;
    
            DateTime val;
            try
            {
                val = DateTime.ParseExact(value.ToString(), Format, null);
            }
            catch(FormatException)
            {
                return false;
            }
    
            //Not entirely sure it'd ever reach this, but I need a return statement in all codepaths
            return val != DateTime.MinValue;
       }
    }
    

    Then it’s just a matter of using it.
    [DateTimeFormat(Format = "dd MMM yyyy")]

    UPDATE: Sorry I donn’t think I clearly read your question. The reason it’s complaining about the data on postback is because the format you’re trying to use is not a standard one. You might be better off implementing one of the common date pickers online to use when populating the field rather than letting it be hand edited or expecting a non-standard format like this. Custom display formats are great for displaying, but if you want to use a custom format for edit mode that the default DateTime.Parse doesn’t understand you’d have to write your own ModelBinder I believe, and that’s something I’d not done, alternatively you could change the data type on your viewmodel to string and the parse it yourself in the action method (you could still use the validator I provided in this case). To get rid of your error (but will also remove your custom format when in edit mode) you’d have to remove the ApplyFormatInEditMode property.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an UnitOfWork attribute, something like this: public class UnitOfWorkAttribute : ActionFilterAttribute {
I use MVC 3 and EF 4.3.1. I have my class created with POCO
I use Spring MVC 3.0 and JSP. I have an object: public class ObjectWrapper
I have a Class like this: class ClassA { public long classAID {get; set;}
I have a view model that looks like this: public class VenueIndexViewModel : BaseViewModel
I have an autogenerated class from importing a web service containing something like this
I have MVC application configured to use Forms Authentication. I'm signing out users by
I have learned how to use Spring MVC 3, but I am very interested
I have been attempting to use ASP.NET MVC remote validation for username login access
I have an MVC application created from the Internet template to use forms authentication,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.