简体   繁体   English

我想将 Javascipt object 传递给 c# 方法而不在 Mono 或 Z98AD8B3C99B3CA416F1F7FA84FEE 中序列化

[英]I want to pass Javascipt object to c# method without serialization in Mono or Blazor

I want to pass the javascript object directly to c# method in Blazor.我想将 javascript object 直接传递给 Blazor 中的 c# 方法。 if i call the GetModel method it is returning the same object which i passed but not the updated object.如果我调用 GetModel 方法,它将返回相同的 object,但不是更新后的 object。 I have followed the below process.我遵循了以下过程。

JavaScript Object 

export interface ITestItem {
  data: string;
  moreData: string;
}

C# Object 

public class ITestItem
{
  public string data { get; set; }
  public string moreData { get; set; }
}

Method in C#:

public static ITestItem GetModel(ITestItem item)
{
  item.moreData = "Get Model Working Fine";
  return item;
}

Calling C# method from JavaScript

public getModel() {
 const getModelFunc = Module.mono_bind_static_method(" 
 [Report]Epicor.MetaFx.Wasm.Dynamic.RunClass:GetModel");
  const item: ITestItem = { data: 'Test', moreData: 'checking Get Model' };
  const output = getModelFunc(item);
  console.log(output);
 }

Looks like this is a TypeScript project in Blazor.看起来这是 Blazor 中的TypeScript项目。

I try the solution with ASP.NET Core 3.1 and it works.我尝试使用 ASP.NET Core 3.1 的解决方案,它可以工作。

Assume your project (Assembly) name is CICalc假设您的项目(程序集)名称是 CICalc

1. Add the exampleJsInterop.ts file in wwwroot folder 1.wwwroot文件夹中添加exampleJsInterop.ts文件

namespace JSInteropWithTypeScript {

    export interface ITestItem {
        data: string;
        moreData: string;
    }
      
    class ExampleJsFunctions {
        public getModel(): void {
            const item: ITestItem = { data: 'Test', moreData: 'checking Get Model' };
            (window as any).DotNet.invokeMethodAsync('CICalc', 'GetModel', item)
            .then(data => {
                console.log(data);
            });
        }
    }

    export function Load(): void {
        window['exampleJsFunctions'] = new ExampleJsFunctions();
    }
}

JSInteropWithTypeScript.Load();

2. Add build Microsoft.TypeScript.MSBuild package reference and compile condition in.csproj 2.在.csproj中添加构建Microsoft.TypeScript.MSBuild package参考和编译条件

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netstandard2.1</TargetFramework>
    <RazorLangVersion>3.0</RazorLangVersion>
    <TypeScriptToolsVersion>3.2</TypeScriptToolsVersion>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="3.2.1" />
    <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Build" Version="3.2.1" PrivateAssets="all" />
    <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="3.2.1" PrivateAssets="all" />
    <PackageReference Include="Microsoft.TypeScript.MSBuild" Version="3.9.7">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="System.Net.Http.Json" Version="3.2.0" />
  </ItemGroup>
  <ItemGroup>
    <TypeScriptCompile Include="wwwroot/exampleJsInterop.ts" />
  </ItemGroup>

</Project>

3. Import the exampleJsInterop.js in index.html. 3.导入index.html中的exampleJsInterop.js It's important that it's .js not .ts because dotnet will compile ts to js .它是.js而不是.ts很重要,因为 dotnet 会将ts编译为js

    <script src="exampleJsInterop.js"></script>

4. In your page, add the button to trigger getModel function and add a static GetModel C# function 4.在你的页面中,添加按钮触发getModel function并添加一个static GetModel C#A38541250Z4D768

<button type="button" class="btn btn-primary"
        onclick="exampleJsFunctions.getModel()">
    Trigger .NET static method
</button>

@code {
    [JSInvokable]
    public static Task<ITestItem> GetModel(ITestItem item)
    {
        item.moreData = "Get Model Working Fine";
        return Task.FromResult(item);
    }

    public class ITestItem
    {
        public string data { get; set; }
        public string moreData { get; set; }
    }
}

5. Finally, js get the result from C# when you click the button. 5.最后,js从C#中获取点击按钮的结果。

Reference: Getting Started with TypeScript for JSInterop in Blazor参考: Blazor 中 JSInterop 的 TypeScript 入门

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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