I have two class parent and child list class which i want to populate from controller; i have some problem when it says object null when i loop data to fill that child list
I hope someone can help me
Thanks Ruben
public class Milestone
{
public long shipment_id
{
get;
set;
}
public string vehicle_name
{
get;
set;
}
public DateTime? shipment_date
{
get;
set;
}
public string shipment_number
{
get;
set;
}
public string origin_geofence_code
{
get;
set;
}
public DateTime? planned_departure_time
{
get;
set;
}
public DateTime? departure_time
{
get;
set;
}
public byte? progress
{
get;
set;
}
public string driver_name
{
get;
set;
}
public List <MilestoneDetail> detail { get; set; }
}
public class MilestoneDetail
{
public long? shipment_destination_id
{
get;
set;
}
public short? number
{
get;
set;
}
public string destination_geofence_name
{
get;
set;
}
public DateTime? arrival_time
{
get;
set;
}
public DateTime? departure_time
{
get;
set;
}
public DateTime? planned_arrival_time
{
get;
set;
}
public DateTime? planned_departure_time
{
get;
set;
}
}
[HttpPost]
public JsonResult GetMilestone(DateTime fromDate, DateTime toDate, string search)
{
List<Milestone> model = new List<Milestone>();
List<ViewShipmentSummary> dt = db.ViewShipmentSummaries.Where(m => m.shipment_date >= fromDate && m.shipment_date <= toDate).ToList();
foreach (var i in dt)
{
Milestone ml = new Milestone();
ml.shipment_id = i.shipment_id;
ml.vehicle_name = i.vehicle_name;
ml.shipment_number = i.shipment_number;
ml.shipment_date = i.shipment_date;
ml.origin_geofence_code = i.origin_geofence_name;
ml.planned_departure_time = i.planned_departure_time;
ml.departure_time = i.departure_time;
ml.driver_name = i.driver_name;
ml.progress = i.progress;
model.Add(ml);
var des = db.ViewShipmentDestinations.Where(m => m.shipment_id == i.shipment_id).OrderBy(m => m.number).ToList();
foreach(var a in des)
{
MilestoneDetail mdetail = new MilestoneDetail();
mdetail.shipment_destination_id = a.shipment_destination_id;
mdetail.number = a.number;
mdetail.destination_geofence_name = a.destination_geofence_name;
mdetail.planned_departure_time = a.planned_departure_time;
mdetail.departure_time = a.departure_time;
mdetail.planned_arrival_time = a.planned_arrival_time;
mdetail.arrival_time = a.arrival_time;
ml.detail.Add(mdetail);
}
}
return Json(model, JsonRequestBehavior.AllowGet);
}
The error that i get is "System.NullReferenceException: 'Object reference not set to an instance of an object.'"
I got error at line 30 when try to add mdetail to ml.detail