首页 > 解决方案 > 如何在 asp.net core mvc 网站上显示来自 firebase 的对象?

问题描述

我正在用 asp.net core MVC 制作一个网站,我应该能够在其中显示商品库存。

我创建了一个 firebase 实时数据库,我应该可以在其中添加更多商品并在网站上显示商品。我现在可以写入数据库以添加商品并且已正确添加。我的问题是我无法从数据库中正确获取数据。

这是在 HomeController 内部:

Merchandise test = new Merchandise("Løbsmærker", 25, test2);

var firebaseClient = new FirebaseClient("https://invictuslagerstyring.firebaseio.com/");
var result = await firebaseClient.Child("Name " + test.Name).PostAsync(test);

var ud = await firebaseClient.Child("Name " + test.Name).Child("Name").OnceAsync<Merchandise>();

这是商品构造函数:

public Merchandise(String name, double salePrice, Boolean[] size)
{
    merchID = ++amountOfMerch;
    this.name = name;
    this.salePrice = salePrice;
    this.size = new Boolean[size.Length];

    if (size.Length > 0)
    {
        this.size = new Boolean[amountOfSizes];
        this.size = size;
    }
}

我希望输出“Løbsmærker”,但实际输出是Firebase.Database.FirebaseObject1[Lagerstyring.Controllers.Merchandise][]

标签: c#asp.net-core

解决方案


我怀疑你运行后:

var ud = await firebaseClient.Child("Name " + test.Name).Child("Name").OnceAsync<Merchandise>();

您将上述变量分配给文本字段或其他内容。哪个调用ToString()方法。而是创建一个新变量并分配Name给它。


推荐阅读