跳到正文
Skillpedia
网站与界面(前端)5.8k · #14562

collect-user-input

它能做什么

这项技能帮助在 Blazor 中构建表单、搜索框和输入界面,包括验证和提交。适合为 Blazor 应用添加数据录入功能的 .NET 开发者。

适用场景

  • ✓当你需要在 Blazor 应用中添加表单、搜索框或筛选面板时。
  • ✓当你希望使用注解或自定义规则对表单进行验证时。
  • ✓当你希望在 Blazor 页面中支持文件上传或行内编辑的时候。

技能链接

https://github.com/dotnet/skills/blob/main/plugins/dotnet-blazor/skills/collect-user-input/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/dotnet/skills/blob/main/plugins/dotnet-blazor/skills/collect-user-input/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“请添加一个 Blazor 注册表单,对姓名和邮箱进行验证,并将用户保存到数据库。”

每周 5 个新技能,1 分钟读完。

工作原理

智能体会读取项目的交互性设置,用适合该模式的方式构建表单,添加验证,并处理提交操作。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

Collect User Input

Step 1 — Read the Project's AGENTS.md

Check AGENTS.md for Interactivity Mode and Interactivity Scope. This determines which form patterns apply:

Mode Form mechanism
None (Static SSR) EditForm with FormName + [SupplyParameterFromForm]. No @bind, no @onchange.
Server EditForm with @bind-Value. Full interactivity — real-time validation, dynamic UI.
WebAssembly Same as Server, but validators needing server data must call APIs.
Auto Same as WebAssembly — code must work in both browser and server.
Scope Impact
Global All forms are interactive. FormName only needed when explicitly opting a page to static SSR.
Per-page Forms in static pages use FormName + [SupplyParameterFromForm]. Forms in @rendermode pages use @bind-Value.

EditForm Setup

EditForm requires either Model or EditContext — never both.

Model-based (default)

<EditForm Model="Employee" OnValidSubmit="HandleSubmit" FormName="employee">
    <DataAnnotationsValidator />
    <ValidationSummary />

    <label>
        Name: <InputText @bind-Value="Employee!.Name" />
        <ValidationMessage For="() => Employee!.Name" />
    </label>

    <button type="submit">Save</button>
</EditForm>

@code {
    [SupplyParameterFromForm]
    private EmployeeModel? Employee { get; set; }

    protected override void OnInitialized() => Employee ??= new();

    private async Task HandleSubmit()
    {
        // Save Employee
    }
}

This single pattern works in both SSR and interactive modes:

  • In SSR: FormName identifies the form, [SupplyParameterFromForm] binds POST data, ??= initializes on GET.
  • In interactive: @bind-Value provides two-way binding, [SupplyParameterFromForm] is ignored, FormName is harmless.

EditContext-based (advanced)

Use when you need programmatic field tracking, dynamic validation rules, or manual EditContext.Validate() calls:

private EditContext? editContext;
private EmployeeModel model = new();

protected override void OnInitialized()
{
    editContext = new EditContext(model);
}
<EditForm EditContext="editContext" OnValidSubmit="HandleSubmit" FormName="employee">

Submit Handlers

Handler Fires when Use when
OnValidSubmit Validation passes Standard forms with DataAnnotationsValidator
OnInvalidSubmit Validation fails Need custom handling for invalid state
OnSubmit Always — validation is manual Using EditContext.Validate() yourself

OnSubmit cannot combine with OnValidSubmit/OnInvalidSubmit.

Built-in Input Components

Component Binds to Notes
InputText string Renders <input type="text">
InputTextArea string Renders <textarea>
InputNumber<T> int, double, decimal Renders <input type="number">
InputDate<T> DateTime, DateOnly, DateTimeOffset Renders <input type="date">
InputCheckbox bool Renders <input type="checkbox">
InputSelect<T> string, enums, numeric types Renders <select>
InputRadioGroup<T> string, enums, numeric types Wraps InputRadio<T> children
InputFile IBrowserFile File upload — interactive modes only

All input components use @bind-Value for binding. Always wrap text in a <label> or use id/for attributes for accessibility.

InputSelect with enum values

<InputSelect @bind-Value="Model!.Status">
    <option value="">-- Select --</option>
    @foreach (var value in Enum.GetValues<OrderStatus>())
    {
        <option value="@value">@value</option>
    }
</InputSelect>

InputRadioGroup

<InputRadioGroup @bind-Value="Model!.Priority">
    @foreach (var p in Enum.GetValues<Priority>())
    {
        <label>
            <InputRadio Value="p" /> @p
        </label>
    }
</InputRadioGroup>

Validation

Data annotations

Define validation rules on the model:

public class EmployeeModel
{
    [Required, StringLength(100)]
    public string? Name { get; set; }

    [Required, EmailAddress]
    public string? Email { get; set; }

    [Range(18, 99)]
    public int Age { get; set; }

    [Required]
    public string? Department { get; set; }
}

Add <DataAnnotationsValidator /> inside EditForm — without it, annotatio

常见问题

它适用于静态页面吗?

适用。它既涵盖交互式页面的表单模式,也涵盖静态服务器页面的表单模式。

它会添加验证吗?

会。它涵盖基于注解的验证和自定义验证。

它涵盖文件上传吗?

涵盖。文件上传是其支持的输入类型之一。

详情

作者的原始描述

Build forms, validate data, and react to user input in Blazor. USE FOR adding forms, search boxes, filter panels, inline editing, data-entry UI, file uploads, validation (annotations or custom), handling form submissions, and binding input controls. Covers EditForm, built-in input components, DataAnnotationsValidator, custom validation, SSR form patterns (SupplyParameterFromForm, FormName, AntiforgeryToken, Enhance), and @bind for simple interactive controls. DO NOT USE for project scaffolding (see create-blazor-project) or prerendering issues (see support-prerendering).

repo: dotnet/skills

热度

GitHub 星标
5.8k
排名
#14562
许可证
MIT
更新时间
2026/11/2

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能