c# - Nested Model returning null in View -
i know question has been asked before struggling find suitable solution. have foreach loop
compares values 2 models , decides parent element each belong can see in view (table has expand/close capability) want each group appear under section have been allocated to, when call item.group.groupsectionid
returns null every time, when debugging controller populating groupdetail
model , sectiondetail
model being populated public groupdetail group { get; set; }
returning null:
view (sectiontable)
<table class="table table-striped"> <tbody> <!-- render details of each employee. --> @foreach (var item in model) { <tr> <td>-</td> <td>@html.displayfor(model => item.name)</td> </tr> if (item.group.groupsectionid != 0 && item.sectionid == item.group.groupsectionid) { <tr> <td colspan="3"><p>@html.displayfor(model => item.group.groupname)</p></td> </tr> } } </tbody> </table>
controllers
public actionresult grouptable() { manager manager = new manager(); var data1 = manager.getallgroups(); var groupdetails = u in data1 select new groupdetail { groupid = u.id, groupdescription = u.description, groupname = u.name, groupsectionid = u.sectionid, sectionname = u.sectionname }; return view(groupdetails.tolist()); } public actionresult sectiontable() { manager manager = new manager(); var data3 = manager.getallsections(); var sectiondetails = u in data3 select new sectiondetail { sectionid = u.id, name = u.name, description = u.description }; return view(sectiondetails.tolist()); }
models
public class sectiondetail { public int sectionid { get; set; } public string name { get; set; } public string description { get; set; } public groupdetail group { get; set; } } public class groupdetail { public int groupid { get; set; } public string groupdescription { get; set; } public string groupname { get; set; } public int groupsectionid { get; set; } public string sectionname { get; set; } }
thanks in advance , if want further info please ask.
you don't assign group property here:
var sectiondetails = u in data3 select new sectiondetail { sectionid = u.id, name = u.name, description = u.description, group = u.group };
Comments
Post a Comment