【问题标题】:EditForm <InputNumber> needs to be entered as int not string, but it wont let meEditForm <InputNumber> 需要输入为 int 而不是字符串,但它不会让我
【发布时间】:2021-12-30 17:54:30
【问题描述】:

您好,我正在学习 razor 和 razor pages;我正在努力将项目添加到一个简单的数据库中。

当我尝试输入一个数量(应该是一个整数)时,我注意到表单中的输入有些奇怪,当我按 1 或任何数字时它会引发错误;并说它应该是字符串/数组/集合?那是怎么回事?

来自 Products.razor 的代码摘录:

@if (products != null) // Insert form 
{
<EditForm Model="@product" OnValidSubmit="@HandleAdd">
  <DataAnnotationsValidator />
  <ValidationSummary />
 
  <InputText placeholder="Product Name" id="ProductName" @bind-Value="@product.ProductName" />
  <br />
  <InputText placeholder="Unit of Measurement" id="Unit" @bind-Value="@product.Unit" />
  <br />
  <InputNumber min="1" step="1" placeholder="Quantity" id="Quantity" @bind-Value="@product.Quantity" />
  <br />
  <InputNumber min="0.01" step="0.01" placeholder="Unit Price" id="UnitPrice" @bind-Value="@product.UnitPrice" />
  <br />
  <InputText placeholder="Category" id="Category" @bind-Value="@product.Category" />
  <br />
  <button type="submit">Submit</button>
</EditForm>

colsole 中的奇怪错误:

我确实注意到有人对一个不相关的问题有想法: Blazor EditForm adding InputNumber fields

但我已经这样做了,但它仍然无法正常工作;以及此表单代码;现在指示有效输入的绿色小边框不亮了。

任何帮助将不胜感激:D

【问题讨论】:

    标签: database forms validation input razor


    【解决方案1】:

    在我的 Product 类的构造中将 [MaxLength] 属性应用于我的 Quantity 时出错。

    删除它,添加新的迁移,现在它可以工作了 :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      • 2011-05-20
      • 2021-06-12
      • 2021-07-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多