1

I have a controller which is returning the list of objects in TempData

 public ActionResult timelineIndex()
    { 
        var jsondata = new
        {
            data = (
                from t in ptr.GetAll()
                select (new
                {
                    id = t.pptid,
                    Owner = t.Owner,
                    Duration = t.totaldays,
                    Comp = t.Status,
                    StartDate = t.StartDate,
                    EndDate = t.EndDate
                }).ToString())
           };
        TempData["id"] = jsondata.data.ToList();


        return View();
    }

Now in view i encoded using @html.Raw(json.Encode)

var modeldata = @Html.Raw(Json.Encode(@TempData["id"])) ;

I want to loop through all objects for that i tried that but it is showing Undefined.

    console.log(modeldata);
    var array = [];
    array = modeldata;
  for (var i = 0 ; i<11 ; i++) {  
      var obj = modeldata[i];
      alert(obj.id);
      alert(obj.Startdate)
  }

in modeldata array of object is coming.

(11) ["{ id = 1, Owner = erewrwer, Duration = 7, Comp = e…w, StartDate = 2018-09-19, EndDate = 2018-09-26 }", "{ id = 2, Owner = erewrwer, Duration = 7, Comp = , StartDate = 2018-09-19, EndDate = 2018-09-26 }", "{ id = 3, Owner = erewrwer, Duration = 11, Comp = , StartDate = 2018-09-19, EndDate = 2018-09-30 }", "{ id = 4, Owner = erewrwer, Duration = 11, Comp = …z, StartDate = 2018-09-19, EndDate = 2018-09-30 }", "{ id = 5, Owner = erewrwer, Duration = 11, Comp = …z, StartDate = 2018-09-19, EndDate = 2018-09-30 }", "{ id = 6, Owner = erewrwer, Duration = 11, Comp = …z, StartDate = 2018-09-19, EndDate = 2018-09-30 }", "{ id = 7, Owner = erewrwer, Duration = 11, Comp = …S, StartDate = 2018-09-19, EndDate = 2018-09-30 }", "{ id = 8, Owner = erewrwer, Duration = 11, Comp = …S, StartDate = 2018-09-19, EndDate = 2018-09-30 }", "{ id = 9, Owner = erewrwer, Duration = 11, Comp = …E, StartDate = 2018-09-19, EndDate = 2018-09-30 }", "{ id = 10, Owner = erewrwer, Duration = -204, Comp…D, StartDate = 2018-11-30, EndDate = 2018-05-10 }", "{ id = 11, Owner = erewrwer, Duration = 6, Comp = …S, StartDate = 2018-09-24, EndDate = 2018-09-30 }"]
Max Langhof
  • 23,383
  • 5
  • 39
  • 72
hira shah
  • 23
  • 1
  • 4
  • This may help you: https://stackoverflow.com/questions/16626735/how-to-loop-through-an-array-containing-objects-and-access-their-properties – enxaneta Sep 27 '18 at 07:10
  • Do this `var obj = JSON.parse(modeldata[i]);` Because whatever in the array you are gettint that is string not an object – Sourabh Somani Sep 27 '18 at 07:14
  • Welcome to SO. It looks like you should tag this C# since this does not seem to be an issue with JavaScript. Maybe you could go to the network tab in your browser, look at the request and post the content of the output (in the response tab). Also show how you are getting modeldata (fetch, xhr, axios?). Seems you are generating JSON array of JSON strings with C# – HMR Sep 27 '18 at 07:15
  • @HMR Thanks for the correction That was my mistake i converted the object into string from c# code.... – hira shah Sep 27 '18 at 07:22
  • If the modeldata array is supposed to be an array of JSON strings then there is a problem. Those are not JSON. JSON would look like `{ "id": 1, "Owner": "erewrwer" }`, not like `{ id = 1, Owner = erewrwer }`. – trincot Sep 27 '18 at 07:22

2 Answers2

2

Javascript is all fine you need to do correction from c# code and it will work fine

 public ActionResult timelineIndex()
{ 
    var jsondata = new
    {
        data = (
            from t in ptr.GetAll()
            select (new
            {
                id = t.pptid,
                Owner = t.Owner,
                Duration = t.totaldays,
                Comp = t.Status,
                StartDate = t.StartDate,
                EndDate = t.EndDate
            }))
       };
    TempData["id"] = jsondata.data.ToList();


    return View();
}
Faizan
  • 542
  • 5
  • 16
-1

Your data formatting is incorrect. Its a JSON data. You have to convert it through JSON.parse. Here is the working example. Check the model array. The data formatting should be look like this.

let modelArray = [
  { id : 1, Owner : "erewrwer", Duration : 7, StartDate : "2018-09-19", EndDate : "2018-09-26" },
];

for (let i = 0; i < modelArray.length; i++) {
  let obj = modelArray[i];
  console.log('Object : ', obj);
  console.log("Id : ", obj.id);
  console.log("StartDate : ", obj.StartDate);
}
Asad Marfani
  • 177
  • 3
  • 13