如何使用 Blazor 创建表单向导?

How do you create a form wizard using Blazor?

如何使用 Blazor + MatBlazor(Material Design for Blazor)而不是 Javascript 创建表单向导?用户应该能够在表单的不同页面之间导航,并且进度条应该动态更新。

以下是受 Louis Hendricks 启发的基本表单向导。

请注意,您需要安装 Vladimir Samoilenko 的 MatBlazor 才能让以 "Mat" 为前缀的网络元素正常工作。

Form.razor 文件

<div id="form-navigation-container">

    <!-- Checks to see if current step is not equal to first step via SetNavButtons() method -->
    <MatButton Class="previous" disabled="@Wizard.PreviousButtonDisabled" @onclick="Wizard.GoToPreviousStep">Previous Step</MatButton>

    <!-- Checks to see if current step is not equal to last step via SetNavButtons() method -->
    <MatButton Unelevated="true" disabled="@Wizard.NextButtonDisabled" @onclick="Wizard.GoToNextStep">Next Step</MatButton>
</div>

<!-- Progress bar gets incremented/decremented via GoToNextStep()/GoToPreviousStep() methods -->
<MatProgressBar Class="progress" Progress="@Wizard.Progress" aria-valuenow="@Wizard.Progress" aria-valuemin="0" aria-valuemax="1"></MatProgressBar>

@code {
    Wizard Wizard = new Wizard();
}

Wizard.cs 文件

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

namespace FORM.Models
{
    public class Wizard
    {


        public Wizard()
        {
            Progress = .2;
            Step = 1;
            SetNavButtons();
        }

        public double Progress { get; private set; }

        public int Step { get; private set; }

        public bool PreviousButtonDisabled { get; private set; }
        public bool NextButtonDisabled { get; private set; }

        public void GoToNextStep()
        {
            Step += 1;            
            Progress += .2;
            if (Step == 6)
            {
                Progress = 100;
            }
            SetNavButtons();
        }

        public void GoToPreviousStep()
        {
            if (Step > 1)
            {
                Step -= 1;
                Progress -= .2;
            }
            if (Step == 1)
            {
                Progress = .2;
            }
            SetNavButtons();
        }

        public void SetNavButtons()
        {
            NextButtonDisabled = false;
            switch (Step)
            {
                case 1:
                    PreviousButtonDisabled = true;               
                    break;
                case 2:
                    PreviousButtonDisabled = false;
                    break;
                case 3:
                    PreviousButtonDisabled = false;
                    break;
                case 4:
                    PreviousButtonDisabled = false;
                    break;
                case 5:
                    NextButtonDisabled = true;
                    break;
                default:
                    break;
            }
        }
    }
}