c# - optional nullable DateTime value in Model -


this model :

public class person {     public int personid {get;set;}     public string name {get;set;}      [datatype(datatype.date)]     [displayformat(dataformatstring = "{0:yyyy-mm-dd}", applyformatineditmode = true)]     public datetime? birthdate {get;set; } } 

in controller want let datetime property value optional, in word user can enter or not enter value property.
how can ?

edit :
dbcontext class:

public class mydbcontext : dbcontext {     public mydbcontext() : base("defaultconnection")     {     }    public dbset<person> persons { get; set; } } 

my action method :

[httppost] [validateantiforgerytoken] public actionresult create([bind(include = "personid,name,birthdate")] person person) {     if (modelstate.isvalid)     {         db.persons.add(person);         db.savechanges(); // error thrown         return redirecttoaction("index");     }      return view(person); } 

exception(thrown when trying create record null datetime value):

an exception of type 'system.data.entity.infrastructure.dbupdateexception' occurred in entityframework.dll not handled in user code  additional information: error occurred while updating entries. see inner exception details. 

inner exeptions:

exception:thrown: "the conversion of datetime2 data type datetime data type resulted in out-of-range value. statement has been terminated." (system.data.sqlclient.sqlexception) system.data.sqlclient.sqlexception thrown: "the conversion of datetime2 data type datetime data type resulted in out-of-range value. statement has been terminated." time: 07/14/2015 08:41:59 ب.ظ thread:worker thread[3028]  exception:caught: "culture not supported." (system.globalization.culturenotfoundexception) system.globalization.culturenotfoundexception caught: "culture not supported." time: 07/14/2015 08:41:42 ب.ظ thread:<no name>[8284] 

note:
right figured out if don't use custom binder , culture every thing works fine.

system.data.entity.infrastructure.dbupdateexception indicates value being passed controller fine, data layer thinks value required. check schema ensure column nullable.


Comments

Popular posts from this blog

Android : Making Listview full screen -

javascript - Parse JSON from the body of the POST -

javascript - Chrome Extension: Interacting with iframe embedded within popup -