簡體   English   中英

在MVC4中將對象列表從控制器傳遞到View

[英]Passing list of objects from controller to View in MVC4

我是ASP / MVC的新手,因此開始使用一個簡單的應用程序學習。

我在控制器中有以下對象的集合

    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在模型中如下

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; }
        }


    }
}

我收到以下意見

@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>

我遇到的問題是,服務器未發送AddlocatioPin,我猜只是忽略了它。

我在做些很愚蠢的事情嗎? 請幫忙

您不應該使用script包裝html標記。 開始和結束標記,它們的順序也必須在html中匹配。 您還應該閱讀有關HTML ul標簽的更多信息

正確的看法是

@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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM