Click here to Skip to main content
15,072,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi. I have a model with a table namely Resource that consist of multiple properties. I want define an array and fill that with Id,Latitude and Longitude properties in action namely DistanceMCalculate() in controller and pass this array to javascript in view in my MVC project.

this is my model in edmx:

public partial class Resource
        public int Id { get; set; }
        public int Code { get; set; }
        public string Name { get; set; }
        public Nullable<System.DateTime> StartHour { get; set; }
        public string Address { get; set; }
        public Nullable<System.DateTime> Arrivetime { get; set; }
        public Nullable<int> ArriveTimeDuration { get; set; }
        public Nullable<int> ProgressPercent { get; set; }
        public Nullable<int> ResourceTypeId { get; set; }
        public Nullable<decimal> Latitude { get; set; }
        public Nullable<decimal> Longitude { get; set; }

        public virtual ResourceType ResourceType { get; set; }

this is my action and I want add Id,Latitude and Longitude properties of model to an arraylist and pass to view in javascript. I need just array in can I do it?

public ActionResult DistanceMCalculate()
            var model= db.Resource.Where(p => p.ResourceTypeId == 1 && p.Latitude != null).ToList();
            return Json();

Thanks a lot

What I have tried:

public JsonResult DistanceMCalculate()
           var origins = db.Resource.Where(p => p.ResourceTypeId == 1 && p.Latitude != null).ToList();
           //decimal?[] array = new decimal?[3];
           ArrayList a = new ArrayList();
           ArrayList list = new ArrayList();
           foreach (var item in origins)
               a[0] = item.Id;
               a[1] = item.Latitude;
               a[2] = item.Longitude;
           //ViewBag.originsArry = origins.ToArray();
           ViewBag.list = JsonConvert.SerializeObject(list);

           return Json(ViewBag.list, JsonRequestBehavior.AllowGet);

I have error in line
ViewBag.list = JsonConvert.SerializeObject(list);
Richard MacCutchan 7-Dec-19 4:23am
What error?
F-ES Sitecore 9-Dec-19 4:58am
Don't use ViewBag, don't use ArrayList. Either create concrete classes that can model your data and build a list of those, or use anonymous types.

var returnData = origins.Select (item => new {
Id = item.Id,
Latitude = item.Latitude,
Longitude = item.Longitude

then convert "returnData" to JSON to return from your method. The issue with your code is that you will find you're just adding the same data over and over and you'll have a list of the last object, and probably also the json converter doesn't have a valid property name to use in json as you are using ArrayList that has no "name" concept.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900