c# - Exception in Automapper Mapping and how to frame the class structure properly -
i facing below issues when try achieve requirement.
in controller have used automapper map "viewmodel" class "entities" class getting exception.i used automapper exception class catch exception, result :
missing type map configuration or unsupported mapping
mapping types:
empdetails -> staffmember org.entities.empdetails -> org.web.models.staffmember
destination path: list`1[0]
source value: org.entities.empdetails
in data access layer when adding value dropdown getting exception , please advise me whether classes formed/designed , me out rid of these issues.
pfb screen shots , codes:
exception screenshots
exception automapper in controller:
exception dropdown in dal:
code:
controller:-
employeestatus ostatusbl = new employeestatus(); // business layer class public actionresult index() { empstatusdetails ostaff = ostatusbl.getempstatusdetails(); staffmemberlist olsit = new staffmemberlist(); mapper.createmap<empstatusdetails, staffmemberlist>(); mapper.map<empstatusdetails, staffmemberlist>(ostaff); // exception occurs here viewbag.statedropdown = ostatusbl.getstatedropdown(); return view(olsit); }
viewmodel:-
public class staffmember { public string fullname { get; set; } public string fulladdress { get; set; } public string statusmessage { get; set; } public datetime datecreated { get; set; } public int userid { get; set; } } public class staffmemberlist { public list<staffmember> staffdetails { get; set; } public status statusdetails { get; set; } public users userdetails { get; set; } } public class status { [required(errormessage = "please select name")] public int userid { get; set; } [required(errormessage = "status message required")] [regularexpression(@"^[\w\d]+$", errormessage = "avoid special characters")] [stringlength(250, errormessage = "exceeds 250 character")] public string statusmessage { get; set; } public datetime datecreated { get; set; } public ienumerable<selectlistitem> dropdownnamelist { get; set; } } public class users { public int userid { get; set; } [required(errormessage = "first name required")] public string firstname { get; set; } [required(errormessage = "last name required")] public string lastname { get; set; } [required(errormessage = "address required")] [regularexpression("^[a-za-z0-9\\s,'-]*$", errormessage = "invalid address")] [stringlength(250, errormessage = "exceeds 250 character")] public string address { get; set; } [required(errormessage = "city required")] [regularexpression("^[a-za-z]+$", errormessage = "invalid city")] [stringlength(150, errormessage = "exceeds 150 character")] public string city { get; set; } [required(errormessage = "state required")] [regularexpression("^[a-za-z]+$", errormessage = "invalid state")] public string state { get; set; } [required(errormessage = "zip required")] [regularexpression("^[0-9]{5}$", errormessage = "invalid zipcode")] public int? zip { get; set; } public datetime datecreated { get; set; } }
entitieslayer:-
public class empdetails { public string fullname { get; set; } public string fulladdress { get; set; } public string statusmessage { get; set; } public datetime datecreated { get; set; } public int userid { get; set; } public statusentity statusdetails { get; set; } } public class empstatusdetails { public list<empdetails> staffdetails { get; set; } public statusentity statusdetails { get; set; } public userentity userdetails { get; set; } } public class statusentity { public int userid { get; set; } public string statusmessage { get; set; } public datetime datecreated { get; set; } public ienumerable<selectlistitem> dropdownnamelist { get; set; } } public class userentity { public int userid { get; set; } public string firstname { get; set; } public string lastname { get; set; } public string address { get; set; } public string city { get; set; } public string state { get; set; } public int? zip { get; set; } public datetime datecreated { get; set; } }
businesslayer:-
staffdetailsdal odal = new staffdetailsdal(); public empstatusdetails getempstatusdetails() { return odal.employeestatusanddetails(); }
dataaccesslayer:-
public empstatusdetails employeestatusanddetails() { empstatusdetails oemp = new empstatusdetails(); // entity layer class list<user> = dbcontext.users.select(o => o).tolist(); list<empdetails> ostafflist = (from usrs in dbcontext.users join stats in dbcontext.statusupdates on usrs.userid equals stats.userid stats.statusmessage !=null select new empdetails { fullname = usrs.firstname + " " + usrs.lastname, fulladdress = usrs.address + "," + usrs.state + "," + usrs.city + "," + sqlfunctions.stringconvert((double)usrs.zip), statusmessage = stats.statusmessage, datecreated = (datetime)stats.datecreated, userid = usrs.userid } ).orderby(x => x.datecreated).tolist<empdetails>(); ienumerable<selectlistitem> olist = (from val in ostafflist select new selectlistitem { text = val.fullname, value = val.userid.tostring() }).tolist(); oemp.staffdetails = ostafflist; oemp.statusdetails.dropdownnamelist = olist; // exception occurs here return oemp; }
you need create mapping collection of staffdetails
Comments
Post a Comment