c# blazor how to pass a List to a page

前端 未结 1 670
旧时难觅i
旧时难觅i 2021-01-22 10:56

I have a second page that receives the following parameter:

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

@Employees.Count

@cod
相关标签:
1条回答
  • 2021-01-22 11:24

    A viable solution to such issue is to implement the state pattern and notification pattern in a service that keep the state of objects and notify subscribing objects of changes. Thus, you can create a service which cache a collection of Employees, allow objects (your main component in this case) to add employees to that collection, and notify subscribers ( newrecord ) of that fact, enabling access to this collection, or still better passing it from the service to the subscriber object in the form of event args object. Here's a basic sample how to implement the state pattern and notification pattern. (note: this sample works perfectly fine, and it was not created for the answer...)

    Child1.razor

    @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.razor

    @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;
     }
    
    }
    

    NotifierService.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();
                    }
                }
            }
          }
    
       }
    }
    

    Startup.ConfigureServices

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

    Hope this works...

    0 讨论(0)
提交回复
热议问题