C# blazor 如何将列表传递到页面

2023-12-02

我有第二个页面接收以下参数:

@page "/newrecord/{Employees}"
@inject HttpClient http

<h1>@Employees.Count</h1>

@code {
    [Parameter]
    public List<Model.Employees> Employees{ get; set; }
}

在我的主页上,当单击如下按钮时,我会传递列表:

List<Model.Employees> selected { get; set; }

private void OnAddRecord()
    {
        NavigationManager.NavigateTo($"newrecord/{this.selected}");
    }

单击按钮时出现错误,我可以看到 URL 的格式如下:

enter image description here

如何将该对象传递到第二页?我是否必须使用 LocalStorage?或者使用其他类型的对象?


解决此类问题的一个可行的解决方案是在服务中实现状态模式和通知模式,以保留对象的状态并向订阅对象通知更改。因此,您可以创建一个服务来缓存雇员集合,允许对象(在本例中是您的主要组件)将雇员添加到该集合中,并通知订阅者( newrecord )该事实,从而允许访问该集合,或者更好以事件参数对象的形式将其从服务传递到订阅者对象。这是如何实现状态模式和通知模式的基本示例。 (注意:这个示例工作得很好,它不是为了答案而创建的......)

Child1.剃刀

@inject NotifierService Notifier
@implements IDisposable

@inject IState State

<hr />
<input type="checkbox" @bind="State.ShowEasterEggs" />Show Easter Eggs
<hr />

<h1>User puts in something</h1>
<input type="text" @bind="@value" />
<button @onclick="@AddValue">Add value</button>

@foreach (var value in Notifier.ValuesList)
{
   <p>@value</p>
}


@code {
private string value { get; set; }

public void AddValue()
{
    Notifier.AddTolist(value);

}

public async Task OnNotify()
{
    await InvokeAsync(() =>
    {
        StateHasChanged();
    });
}


protected override void OnInitialized()
{
    Notifier.Notify += OnNotify;
}
public void Dispose()
{
    Notifier.Notify -= OnNotify;
}
}

Child2.剃刀

@inject NotifierService Notifier
@implements IDisposable

@inject IState State

<hr />
@if (State.ShowEasterEggs)
{
  <span>EASTER EGGS SHOWN</span>
}

<hr />

<h1>Displays Value from service and lets user put in new value</h1>

<input type="text" @bind="@value" />

<button @onclick="@AddValue">Set Value</button>

@code {
  private string value { get; set; }


 public void AddValue()
 {
    Notifier.AddTolist(value);

 }

 protected override void OnInitialized()
 {
    State.Notify += OnNotify;

 }

 public void OnNotify()
 {
    InvokeAsync(() =>
    {
        StateHasChanged();
    });
 }


 public void Dispose()
 {
    State.Notify -= OnNotify;
 }

}

通知服务.cs

 using System;
 using System.Collections.Generic;
 using System.Net.Http;
 using System.Threading.Tasks;
 using ChildComponentsCommunication.Shared;
 using Microsoft.AspNetCore.Components;

 namespace ChildComponentsCommunication
{
    public class NotifierService
    {
        private readonly List<string> values = new List<string>();
        public IReadOnlyList<string> ValuesList => values;

        public NotifierService()
        {

        }

        public async void AddTolist(string value)
        {
            values.Add(value);
            if (Notify != null)
            {
               await Notify?.Invoke();
            }

        }

        public event Func<Task> Notify;
  }
}

IState.cs

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Threading.Tasks;

 namespace ChildComponentsCommunication
{
  public interface IState
  {
    event Action Notify;
    bool ShowEasterEggs { get; set; }
  }
  public class State : IState
  {
    public event Action Notify;

    bool showEggs = false;
    public bool ShowEasterEggs
    {
        get => showEggs;
        set
        {
            if (showEggs != value)
            {
                showEggs = value;

                if (Notify != null)
                {
                    Notify?.Invoke();
                }
            }
        }
      }

   }
}

启动.配置服务

 services.AddSingleton<IState, State>();
 services.AddScoped<NotifierService>();

希望这有效...

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

C# blazor 如何将列表传递到页面 的相关文章

随机推荐