简体   繁体   中英

Passing list of objects from controller to View in MVC4

I am new to ASP/MVC and as part of that started learning using a simple app.

I have a collection of objects as below in the controller

    public ActionResult loadimage(String FQDN, String trange)
        {
            List<geo_crd> Geo_crd = new List<geo_crd>();

              //more logic


foreach (ToDoItem T in query1)
            {

                IEnumerable<GeoItem> query2 = (from b in db1.GeoItems
                                               where b.DNS_server_address == T.DNS_server_address
                                               select b);
                foreach (GeoItem X in query2)
                {

                    Geo_crd.Add(new geo_crd(X.DNS_latitude, X.DNS_longitude, 1));
                }

            }


            return View(Geo_crd);
        }

Geo_crd is in models as follows

namespace ToDoApp.Models
{
        public class geo_crd 
    {

        private Decimal _geo_lat;
        private Decimal _geo_long;
        private int _status_flag;

        public geo_crd(Decimal x, Decimal y, int z)
        {
            _geo_lat = x;
            _geo_long = y;
            _status_flag = z;
        }

        public Decimal geo_lat
        {
            get { return _geo_lat; }
            set { _geo_lat = value; }
        }

        public Decimal geo_long
        {
            get { return _geo_long; }
            set { _geo_long = value; }
        }

        public int status_flag
        {
            get { return _status_flag; }
            set { _status_flag = value; }
        }


    }
}

I am receiving in the views as follows

@model IEnumerable <ToDoApp.Models.geo_crd>
// more code here 
<script type="text/javascript"> 

    @foreach (var item in Model){ 
            <spam><li> @item.geo_lat </li></spam>
            <span> <li> AddLocationPin(@item.geo_lat, @item.geo_long, null, 'place 1');</li> </span> 
           } 

  </script>

the issue I am having is , the server is not sending the AddlocatioPin , it is just ignoring it I guess.

am i doing something really stupid ? please help

You should not wrap html tags with script . Start and end tags, also their order must match in html. Also you should read more about HTML ul tag

Correct view would be

@model IEnumerable <ToDoApp.Models.geo_crd>
//more code here 
<ul>
   @foreach (var item in Model)
   { 
       <li><span>@item.geo_lat </span></li>
       <li><span>AddLocationPin(@item.geo_lat, @item.geo_long, null, 'place 1'); </span> </li>
   } 
</ul>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM