MVC C# 将项目添加到列表<>在 foreach 内

发布于 2024-10-29 23:42:58 字数 1328 浏览 1 评论 0 原文

我正在做一些事情,虽然我通常对 c# 没有任何问题,但我的大脑崩溃了,有点卡住了......

在我的 MVC 应用程序中,我有一个控制器,它管理 Google 地图的一些标记(如下所示)

   public ActionResult GetMarkers()
    {
        MarkerList markers = getMarkersForMap();

        return Json(markers, JsonRequestBehavior.AllowGet);

    }

    public MarkerList getMarkersForMap()
    {
        MarkerList ml = new MarkerList();

        foreach (var shop in dirRepo.getAllShops())
        {
            Marker marker = new Marker
            {
                html = shop.ShopName,
                lat = shop.Lat,
                lng = shop.Lng,
                label = shop.DirectoryID.ToString()
            };

            ml.markers.Add(marker); // <<<< Object ref not set to an instance of an object

        }
        return m;            
    }

我还有一个标记控制器,如下所示:

public class MarkerList
{
    public List<Marker> markers { get; set; }
}

public class Marker
{
    public string lat { get; set; }
    public string lng { get; set; }
    public string html { get; set; }
    public string label { get; set; }
}

在 foreach 中填充数据没有问题,但我想将多个标记添加到列表中,以便我可以将它们传递回调用函数并继续在我的视图页面中显示结果。我可以通过这样做添加单个项目没有问题

return new MarkerList {markers = new List<Marker> {marker}};

,但是多个项目怎么样?

I'm working on something and while I usually have no problems with c# i've had a brain meltdown and got a bit stuck...

Within my MVC app I have a controller which manages some markers for Google Maps (as follows)

   public ActionResult GetMarkers()
    {
        MarkerList markers = getMarkersForMap();

        return Json(markers, JsonRequestBehavior.AllowGet);

    }

    public MarkerList getMarkersForMap()
    {
        MarkerList ml = new MarkerList();

        foreach (var shop in dirRepo.getAllShops())
        {
            Marker marker = new Marker
            {
                html = shop.ShopName,
                lat = shop.Lat,
                lng = shop.Lng,
                label = shop.DirectoryID.ToString()
            };

            ml.markers.Add(marker); // <<<< Object ref not set to an instance of an object

        }
        return m;            
    }

I also have a Markers controller as follows:

public class MarkerList
{
    public List<Marker> markers { get; set; }
}

public class Marker
{
    public string lat { get; set; }
    public string lng { get; set; }
    public string html { get; set; }
    public string label { get; set; }
}

Data is being populated in the foreach no problem, but I want to add the multiple markers into the list so I can pass them back to the calling function and carry on with displaying the results in my view page. I can add single items no problem by doing this

return new MarkerList {markers = new List<Marker> {marker}};

but how about multiple items??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

老旧海报 2024-11-05 23:42:58

您需要在 MarkerList 中初始化列表才能使用它。您可以在默认的 MarkerList 构造函数中初始化它。

public class MarkerList
{
    public List<Marker> markers { get; set; }

    public MarkerList()
    {
        markers = new List<Marker>();
    }
}

然后,您确定内部列表始终已初始化。

顺便说一句:为什么要将列表封装在仅包含此列表的对象中?现在 MarkerList 类没有任何好处。除非这只是为了满足问题的需要而缩短,否则最好删除此类并直接使用 List

You need to have initialized list in your MarkerList to use it. You can initialize it in the default MarkerList constructor.

public class MarkerList
{
    public List<Marker> markers { get; set; }

    public MarkerList()
    {
        markers = new List<Marker>();
    }
}

Then, you are sure the inner list is always initialized.

BTW: Why do you want to encapsulate your list in the object that contains this list only? There's no benefit of the MarkerList class now. Unless this is only shortened for question's needs, it'll be better to remove this class and use List<Marker> directly.

旧情勿念 2024-11-05 23:42:58

ml.markers 为 null,因为您正在使用自动属性(没有后备存储)。例如,您需要在 ctor 中初始化它。

但有一件事看起来很奇怪。首先,为什么标记是公开的并且不以大写字母开头。更重要的是。当您访问内部列表时,MarkersList 类的作用是什么?现在看来没有多大意义。将内部标记列表设置为私有/受保护,并将 AddMarker 方法添加到 MarkersList。或者只是在控制器中使用 List 并返回它。

ml.markers is null because You are using auto properties (without backing store). You need to initialize it in ctor for example.

But one thing looks weird. First why is markers public and not started with capital letter. And more important. What is MarkersList class for when You access inner List? Right now it doesn't make much sense. Either make the inner markerslist private/protected and add AddMarker method to MarkersList. Or just use List in controller and return it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文