none
How can I fill array with model properties in controller action and pass to view? RRS feed

  • Question

  • 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 view.how 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





          


    • Moved by CoolDadTx Wednesday, December 11, 2019 3:56 PM ASP.NET related
    Friday, December 6, 2019 10:03 PM

All replies

  • ASP.NET MVC can be discussed at the MVC forum in ASP.NET forums.

    http://forums.asp.net/

       

    Saturday, December 7, 2019 6:44 AM
  • ASP.NET MVC can be discussed at the MVC forum in ASP.NET forums.

    Note that this is NOT an MVC question despite the fact that the author mentioned that the resulting code will be used within an MVC application. But the underlying problem is actually a C# question (extracting certain data from a list using the Linq extension methods), so it is correctly asked in this C# forum.

    To extract Latitude and Longitude from your Resource, given that you are already using a linq query, you can add a Select call in your query:

    public ActionResult DistanceMCalculate()
    {
        var model= db.Resource.Where(p => p.ResourceTypeId == 1 && p.Latitude != null).Select(x => new {x.Latitude, x.Longitude}).ToList();
        string json = JsonConvert.SerializeObject(model);
        return View("NameOfView", json);
    }

    This will pass the json as model to the view, i.e., the type of the @model will be string. You can pass it to javascript to deserialize into a variable and it will contain the array of latitudes and longitudes.

    In the previous code, JsonConvert.SerializeObject is what we would use it the project used the Newtonsoft library, but of course you should replace this with a call to whatever Json library you are using in your project.


    Saturday, December 7, 2019 6:38 PM
  • @Alberto

    I doubt that the OP can accomplish any of what you're talking about without going to the MVC forum and being guided through it. :)

    Saturday, December 7, 2019 7:57 PM
  • Hi. Thanks a lot. I need just array in view. because I am using web service that need array. and I should pass this array to javascript in view. How can I do it?

    Thanks

    Sunday, December 8, 2019 6:13 PM
  • Hi. Thanks a lot. I need just array in view. because I am using web service that need array. and I should pass this array to javascript in view. How can I do it?

    Thanks

    So now you have an array of  objects and a Web service that must be addressed by JavaScript. Why did you even mention ASP.NET MVC? What you should be using is Ajax, which is used by JavaScript to access the Web service. By using Ajax, that should allow you to work JavaScript and the HTML view.

    You know where to go, right?

    http://forums.asp.net/

    Sunday, December 8, 2019 7:54 PM
  • Hi Nazanin.Eb,
    First use JsonConvert.SerializeObject() to serialize the object and store it in ViewBag, then you can use @Html.Raw() to get the array in js. Here is a code example you can refer to.

    Controller:
    public ActionResult Index()
            {
                List<Information> informations = new List<Information>
                {
                    new Information{FirstName = "A", Surname = "a"},
                    new Information{FirstName = "B", Surname = "b"},
                    new Information{FirstName = "C", Surname = "c"},
                };
                ViewBag.Info = JsonConvert.SerializeObject(informations);
                return View();
            }
    
    View:
    @section scripts{
        <script>
            var x = @Html.Raw(@ViewBag.Info);
            console.log(x);
            
        </script>
        
    }

    The result,

    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, December 10, 2019 7:58 AM
  • Modify the Controller Method to return Json(json) instead of View("NameOfView", json), and you can access it with "<baseUrl/Controller/MethodName" in Ajax call.
    Tuesday, December 10, 2019 8:37 AM
  • This is clearly an MVC questions because it is talking about getting an array to a view. This is off topic for the C# forums and should be posted in the ASP.NET forums.

    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, December 11, 2019 3:55 PM
  • Hi.because I should pass array from controller action(MVC) to view.
    Wednesday, December 11, 2019 8:39 PM
  • Hi. thank you

    I made the following list:

     var result = db.Resource
                 .Where(p => p.ResourceTypeId == 1 && p.Latitude != null)
                 .Select(x => new { x.Id, x.Latitude, x.Longitude }).ToList();

    and 

    ViewBag.result= JsonConvert.SerializeObject(result);

    return view();

    and 

    var origin = @Html.Raw(@ViewBag.result)
            console.log(origin);

    but I get undefine for origin in javascript.

    Thursday, December 12, 2019 8:29 PM