locked
Pass Array of items to Jquery in asp.net or asp.net mvc RRS feed

  • Question

  • I have the following array structure in my jquery code hardcoded as follows which is basically map coordinates etc.

     var locations = [
                    ['Petronas Twin Tower','3.1579','101.7116'],
                    ['Kuala Lumpur Tower','3.152866','101.7038'],
                    ['Kuala Lumpur Bird Park','3.14312','101.68835'],
                    ['National Mosque of Malaysia','3.141692','101.691645'],
                    ['Merdeka Square','3.147847','101.693433']
            ];

    Obviously the above is hardcoded and I want to be able to do is pass back a dynamic list array, that can be used within my jquery in place of the list above.

    How would I accomplish this using C# in asp.net forms or asp.net mvc? Thanks

    • Moved by CoolDadTx Thursday, February 1, 2018 3:45 PM ASP.NET related
    Wednesday, January 31, 2018 9:45 PM

All replies

  • You would make a custom object, a view model, that would hold a List<T> of custom objects called Locations and you pass the view model into the view. You would load the List<Locations>  in the C# code.

    https://www.wiliam.com.au/wiliam-blog/asp-net-mvc-5-view-model-collection-binding

    MVC and JQuery can be discussed at the ASP.NET forums.

    http://forums.asp.net/

    Wednesday, January 31, 2018 10:11 PM
  • Create a class as follows,

        public class Location
            {
                public string Name { get; set; }
                public long Latitude { get; set; }
                public long Longitude { get; set; }
            }
    Then you can create a collection List<Location> and add Items to it. You can pass the above model to the view!


    Microsoft MVP, Full stack developer, Top contributor Stackoverflow ,@Angular Enthusiast profile for Sajeetharan at Stack Overflow, Q&A for professional and enthusiast programmers

    Thursday, February 1, 2018 1:57 AM
  • Please post questions related to ASP.NET in the ASP.NET forums.

    Michael Taylor http://www.michaeltaylorp3.net

    Thursday, February 1, 2018 3:45 PM