c# - How to handle null datetime appearing in MVC model -


i have controller action performs sql query , passes list of model type view. view outputs simple table data contained in each model.

my model:

public class usermodel {     [key]     public string userid { get; set; }     public string emailaddress { get; set; }     public string password { get; set; }     public string securitytoken { get; set; }     [displayformat(dataformatstring = "{0:yyyy/mm/dd}", applyformatineditmode = true)]     public datetime createddate { get; set; }     [displayformat(dataformatstring = "{0:yyyy/mm/dd}", applyformatineditmode = true)]     public datetime lastuseddate { get; set; }     [displayformat(dataformatstring = "{0:yyyy/mm/dd}", applyformatineditmode = true)]     public datetime lastupdateddate { get; set; } } 

my view:

@foreach (var item in model) {     <tr>         <td>             @html.displayfor(modelitem => item.emailaddress)         </td>         <td>             @html.displayfor(modelitem => item.securitytoken)         </td>         <td>             @html.displayfor(modelitem => item.createddate)         </td>         <td>             @html.displayfor(modelitem => item.lastuseddate)         </td>         <td>             @html.displayfor(modelitem => item.lastupdateddate)         </td>     </tr> } 

for rows in data, lastuseddate , lastupdated date null, resort 1900-01-01, , displayed in view such.

is there way not show value if 1900-01-01?

my query in controller is

select userid, emailaddress, securitytoken, createddate,  isnull(lastuseddate, '1900-01-01') lastuseddate,  isnull(lastupdateddate, '1900-01-01') lastupdateddate  table1 order createddate desc 

and assign them model so:

var user = new usermodel {     createddate = (datetime)reader["createddate"],     lastuseddate = (datetime)reader["lastuseddate"],     lastupdateddate = (datetime)reader["lastupdateddate"] } 

change property type datetime nullable datetime(datetime?).

public class usermodel {     // existing properties goes here      public datetime? lastupdateddate { get; set; } } 

now in sql, not replace null date time value(ex : 1900-01-01). return null.

select userid, emailaddress, securitytoken, createddate,lastupdateddate table1  order createddate desc 

when read data reader, null check. if value not null, assign lastupdateddate property.

var user = new usermodel {     emailaddress = reader.getstring(reader.getordinal("emailaddress")),     createddate = reader.getdatetime(reader.getordinal("createddate"))    } if(!reader.isdbnull(reader.getordinal("lastupdateddate"))) {   user.lastupdateddate = reader.getdatetime(reader.getordinal("lastupdateddate")); } 

Comments

Popular posts from this blog

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

asp.net - Problems sending emails from forum -