当我在Blazor中使用<InputText/>
组件时,会出现以下错误:
Error: System.NullReferenceException: Object reference not set to an instance of an object.
at UniversityApplication.Pages.Shared.AddUserForm.BuildRenderTree(RenderTreeBuilder __builder)
at Microsoft.AspNetCore.Components.ComponentBase.<.ctor>b__6_0(RenderTreeBuilder builder)
at Microsoft.AspNetCore.Components.Rendering.ComponentState.RenderIntoBatch(RenderBatchBuilder batchBuilder, RenderFragment renderFragment)
at Microsoft.AspNetCore.Components.RenderTree.Renderer.RenderInExistingBatch(RenderQueueEntry renderQueueEntry)
at Microsoft.AspNetCore.Components.RenderTree.Renderer.ProcessRenderQueue()
window.console.error @ console.js:223
e.log @ blazor.server.js:15
C @ blazor.server.js:8
(anonymous) @ blazor.server.js:8
(anonymous) @ blazor.server.js:1
e.invokeClientMethod @ blazor.server.js:1
e.processIncomingData @ blazor.server.js:1
connection.onreceive @ blazor.server.js:1
i.onmessage @ blazor.server.js:1
我使用的是3.1.101 dotnet核心版
您必须在剃刀页面中使用这样的InputText,您可以将其用作引用:
<EditForm Model="@SearchTestCoupon">
<div class="card-outer">
<div class="row align-items-center">
<div class="form-group col-md-4 col-lg-4">
<label>Lot no</label>
<InputText @bind-Value="SearchTestCoupon.LotNo" class="form-control"></InputText>
</div>
<div class="form-group col-md-4 col-lg-4">
<label>Material ID</label>
<InputText @bind-Value="SearchTestCoupon.MaterialId" class="form-control"></InputText>
</div>
<div class="form-group col-md-4 col-lg-4">
<button type="button" class="btn btn-primary ml-2 mt-3" @onclick="SearchTestCouponClicked">
<span>Search</span>
</button>
<button type="button" class="btn btn-secondary ml-2 mt-3" @onclick="ClearSearch">
<span>Clear</span>
</button>
</div>
</div>
</div>
</EditForm>
按照这个,你就可以走了。
- 如果要访问InputText或InputNumber,则必须使用EditForm
- 您应该将模型变量分配给EditForm
- 您必须使用该模型将值绑定到InputText