除非序列化为JSON,否则将OBJ传递给JS空的obj。其他对象有效

发布于 2025-01-26 23:40:50 字数 1079 浏览 3 评论 0原文

从本质上讲,我只能将原始类型和匿名类型作为与JS Interop Interop Interop中的参数传递。

我在堆栈溢出上的第一篇文章是因为我无法弄清楚这一点:

此示例有效:

C#:

 async void doThing()
{
  await JSRuntime.InvokeAsync<object>("test", new { Name = "John", Age = 35 });
}

JS:

 var test = funciton(stuff){
  console.log(stuff);
  return stuff;
}

Output in JS is object with name and age as expected.

现在是我的实际班级:

 public class CarsAndBikes
    {
        public car[] cars;
        public bike[] bikes;
    }

 async void doThing()
{
  await JSRuntime.InvokeAsync<object>("test", carsAdnBikes);
}

它不是那么复杂,而是我在JS中看到的是一个空的对象,除非我将其序列化为JSON和挑战。汽车和自行车是课程。 C#

JsonConvert.SerializeObject(CarsAndBikes);

JS:

JSON.parse(stuff)

然后可以正常工作。我不知道为什么我不能将我的班级交给JS,有人知道我是在做错事还是一个错误?

Essentially I can only pass primitives and anonymous types as argument in Blazor interop with JS.

My first post on stack overflow ever because I just cant figure this out:

This example works:

C#:

 async void doThing()
{
  await JSRuntime.InvokeAsync<object>("test", new { Name = "John", Age = 35 });
}

JS:

 var test = funciton(stuff){
  console.log(stuff);
  return stuff;
}

Output in JS is object with name and age as expected.

Now here's my actual class:

 public class CarsAndBikes
    {
        public car[] cars;
        public bike[] bikes;
    }

 async void doThing()
{
  await JSRuntime.InvokeAsync<object>("test", carsAdnBikes);
}

Its not that complex but what I can see in JS is an empty object, unless I serialise it as JSON and deserialise. car and bike are classes.
C#

JsonConvert.SerializeObject(CarsAndBikes);

JS:

JSON.parse(stuff)

Then it works fine. I have no idea why I cannot pass my class to JS, does anyone know if Im doing something wrong or its a bug?

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

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

发布评论

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

评论(1

年少掌心 2025-02-02 23:40:50

这是一个使用您的代码的简单工作演示。它有效,因此您没有向我们展示会破坏您的代码。 CAR自行车看起来像什么?

@page "/"
@inject IJSRuntime Js

<PageTitle>Index</PageTitle>

<button class="btn btn-primary" @onclick=doThing>Object</button>
<button class="btn btn-secondary" @onclick=doBookThing>Book as Object</button>
<button class="btn btn-dark" @onclick=doBook>Book</button>
<button class="btn btn-info" @onclick=doBooks>Books</button>
@code
{
    async void doThing()
    {
        await Js.InvokeAsync<object>("test", new { ID = 1, Title = "Fred" });
    }

    async void doBookThing()
    {
        await Js.InvokeAsync<object>("test", new Book { ID = 1, Title = "Portugal" });
    }
    async void doBook()
    {
        await Js.InvokeAsync<Book>("test", new Book { ID = 1, Title = "Shaun" });
    }

    async void doBooks()
    {
        await Js.InvokeAsync<object>("test", new Catalog());
    }

    public class Book
    {
        public int ID { get; set; }
        public string Title { get; set; } = string.Empty;
    }

    public class Catalog
    {
        public List<Book> Books { get; set; } = new List<Book> {
            new Book { ID=10, Title="Jon" },
            new Book { ID=11, Title="Spain" }
        };
    }
}

Here is a simple working demo using your code. It works, so there's something your not showing us that breaks your code. What does Car and Bike look like?

@page "/"
@inject IJSRuntime Js

<PageTitle>Index</PageTitle>

<button class="btn btn-primary" @onclick=doThing>Object</button>
<button class="btn btn-secondary" @onclick=doBookThing>Book as Object</button>
<button class="btn btn-dark" @onclick=doBook>Book</button>
<button class="btn btn-info" @onclick=doBooks>Books</button>
@code
{
    async void doThing()
    {
        await Js.InvokeAsync<object>("test", new { ID = 1, Title = "Fred" });
    }

    async void doBookThing()
    {
        await Js.InvokeAsync<object>("test", new Book { ID = 1, Title = "Portugal" });
    }
    async void doBook()
    {
        await Js.InvokeAsync<Book>("test", new Book { ID = 1, Title = "Shaun" });
    }

    async void doBooks()
    {
        await Js.InvokeAsync<object>("test", new Catalog());
    }

    public class Book
    {
        public int ID { get; set; }
        public string Title { get; set; } = string.Empty;
    }

    public class Catalog
    {
        public List<Book> Books { get; set; } = new List<Book> {
            new Book { ID=10, Title="Jon" },
            new Book { ID=11, Title="Spain" }
        };
    }
}

enter image description here

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