asp.net web api - Handle NullObjects across two models web api -


i have 2 models in asp.net web api. 1 database model, , other model end user passes in , map properties this:

public static individualproc toindividualnternal(this assignmentexternal item) {             return new individualproc() {                 individualid = (int)item.person.id,                  eventid = (int)item.event.id,                 eventscehduleid = item.schedule.id,                 eventgroupid = item.group.id             }; } 

the problem when user passes null, exception; "nullable object must have value". how can cast nullable properties exception not caused?

what's nullable? purpose of answer i'm going assume nullable int:

item.event.id 

and regular int:

eventid 

in case, can't directly cast former latter because there's no way int handle case of null value. nullable<t> structure has properties check value:

eventid = item.event.id.hasvalue ? item.event.id.value : default(int) 

this check if nullable int has value and, if does, use value. if doesn't, use default value int (which 0).


Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

android - Associate same looper with different threads -

visual studio 2010 - Connect to informix database windows form application -