asp.net mvc - Change DateTime culture info for specific datetime input -


i struggling hours, have model property

public datetime validto { get; set; } 

i display property in view changed format

@html.textboxfor(m => model.validto, "{0:dd.mm.yyyy}" }) 

and problem begin when submit form, because property model.validto has value dd/mm/yyyy. day , month switched position. should mm/dd/yyyy.

how map custom date format in view date format in model ? can set custom date format in model ? cannot set culture whole page, need 1 model or better specific datetime property in model.

any idea ?

experiment 1 - custome model data binder - not work

i tried solution answer displayformat applyformatineditmode

[datetimeformat(format = "dd.mm.yyyy")]         public datetime validto { get; set; }   public class datetimeformatattribute : validationattribute     {         public string 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, need return statement in codepaths             return val != datetime.minvalue;         }     } 

but in view, default datetime format displayed , after submitting dd.mm.yyyy format, model behave same.

try add annotation :

[displayformat(applyformatineditmode = true, dataformatstring = "{0:mm/dd/yyyy}")] 

Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -