【问题标题】:Blazor component not accepting passed contents through parametersBlazor 组件不接受通过参数传递的内容
【发布时间】:2022-08-16 22:39:27
【问题描述】:

我正在使用这个模态组件:https://blazorrepl.telerik.com/wwkzbjaz45yvMAC202

这是代码的样子:

<h4>Contact List</h4>
<table>
    <thead>
        <th>Name</th>
        <th>Email</th>
    </thead>
    <tbody>
        @foreach (var item in contacts!)

        {
            <tr>
                <td>@item.Name</td>
                <td>@item.Email</td>
                <button @onclick= \"@(()=>OnConfirm(item.Name))\">Delete</button>
            </tr>



        }
    </tbody>
</table>
<ModalDialog OnDelete=\"DeleteContact\" @ref=\"modalDialog\"></ModalDialog>

@code {

    private ModalDialog modalDialog;
    private bool showContacts = true;
    private List<Contact>? contacts = null;

  
    protected override void OnInitialized()
    {
        contacts = new List<Contact>(){
        new Contact(){Name=\"John \",Email=\"john@example.com\"},
        new Contact(){Name=\"Bob\",Email=\"bob@example.com\"},
        new Contact(){Name=\"Bill\",Email=\"bill@example.com\"}
};

        base.OnInitialized();

    }

  public void OnConfirm(string name){
    
      modalDialog.Name=name;
      modalDialog.ChildContent=$\"Do you want to delete {name}\";
      modalDialog.Show();
    }
    public void DeleteContact(){
    
    var c=contacts?.Find(x=>x.Name==modalDialog.Name);
    contacts.Remove(c);
    StateHasChanged();
      modalDialog.Close();
    }
 

}

这就是您启动组件的方式:

<ModalDialog OnDelete=\"DeleteContact\" @ref=\"modalDialog\"></ModalDialog>

事实上,它工作得很好,直到我尝试在

  <ModalDialog OnDelete=\"DeleteContact\" @ref=\"modalDialog\">
    <div> hello </div>
  <ModalDialog> 

我收到以下错误:

Error   CS1660  Cannot convert lambda expression to type \'string\' because it is not a delegate type Inest   C:\\....\\Microsoft.NET.Sdk.Razor.SourceGenerators\\Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator\\Components_TestedBy_razor.g.cs   222 Active

    标签: blazor blazor-server-side


    【解决方案1】:

    您正在混合string ChildContentRenderFragment ChildContent

    我建议

    1. 将当前参数从 ChildContent 重命名为 Message
    2. 添加RenderFragment ChildContent 以支持&lt;div&gt; hello &lt;/div&gt; 案例。
      [Parameter]  public string Message { get; set; }
      [Parameter]  public RenderFragment ChildContent { get; set; }
      

      然后决定在对话框中的何处显示 @Message 和 @ChildContent

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-17
      • 2021-01-18
      • 1970-01-01
      • 1970-01-01
      • 2023-03-28
      • 1970-01-01
      • 2021-07-07
      • 1970-01-01
      相关资源
      最近更新 更多