简体   繁体   English

c# blazor 服务器 - 将 class object 从孩子传给父母

[英]c# blazor server - pass class object from child to parent



Hello你好

I am trying to make a reusable component in blazor server我正在尝试在 blazor 服务器中制作一个可重用的组件

A list box, where I pass an id (which it used to populate itself, and then what the user selects is passed back一个列表框,我在其中传递一个 id(它用于填充自身,然后将用户选择的内容传回

My idea is to pass an ID to a component, from a parent to child, which works fine, but I can only get a string back from the child and I would like to send a class object back, is it possible, I have tried the below我的想法是将一个 ID 传递给一个组件,从父级传递给子级,这工作正常,但我只能从子级那里得到一个字符串,我想发送一个 class object 回来,这可能吗,我试过了下面




parent父母

@using Microsoft.AspNetCore.Components.Web

@page "/"

@code {

    private void IwasGivendatabackfromclient(HagClass x)
    {
        string text = x.parChildSet;
        
    }


}


<h1>Hello, world!</h1>

Welcome to your new app.

<HagChild GiveMeDatFromPar="Balls of steel" OnEmployeeDeleted="IwasGivendatabackfromclient"></HagChild>



Class Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components.Web;

namespace Blazor_PH_Par_Child.Shared
{
    public class HagClass
    {
        public  string parSentMe { get; set; }
        public string parChildSet { get; set; }

    }
}



Child孩子

@code {


    [Parameter]
    public string GiveMeDatFromPar { get; set; }

    [Parameter]
    public EventCallback<object> OnEmployeeDeleted { get; set; }

    HagClass x = new HagClass();

    public void Delete_Click()
    {
        x.parChildSet = "test";

        OnEmployeeDeleted.InvokeAsync(x);
        


        }
}


<h3>HagChild</h3>

<button type="button" class="btn btn-danger m-1"
        @onclick="Delete_Click">
    Delete
</button>

<p>hello @GiveMeDatFromPar</p>

Yes it is possible.是的,有可能。 You can add Parameter like this您可以像这样添加参数

[Parameter] 
public EventCallback<MyModel> OnEmployeeDeleted { get; set; }

Then after the button that deletes clicked, you create the item type of Mymodel and send it back to parent然后单击删除按钮后,创建 Mymodel 的项目类型并将其发送回父级

MyModel model = new(){  .... }
OnEmployeeDeleted.InvokeAsync(model);

In parent page you have to create function that gets the child data.在父页面中,您必须创建 function 来获取子数据。

private void GetDeletedInfo(Mymodel model){}

The call of component will be组件的调用将是

<HagChild OnEmployeeDeleted="data => GetDeletedInfo(data)" />

@Baskovli @Quango need help here, I'm creating a user control to import csv file using csvHelper, in my case MyModel will be always different how I can achieve it? @Baskovli @Quango 在这里需要帮助,我正在创建一个用户控件以使用 csvHelper 导入 csv 文件,在我的情况下, MyModel将始终不同,我如何实现它? any help will be appriciated任何帮助都会得到帮助

[Parameter] public EventCallback<MyModel> OnEmployeeDeleted { get; set; }

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

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