[英]thousands separator for a decimal field in a custom inputText component in Blazor
我在 blazor 中有一个自定义 InputText 控件,它可以很好地用于文本输入,但是如果我将它绑定到十进制字段,它就不起作用,并且我会收到类型转换错误
这是我的代码:
这是我的模型
public class Quote
{
[Required]
public string TotalQuoteCost { get; set; }
}
这是我的自定义 InputText
@using System.Linq.Expressions
@inherits InputBase<string>
<div class=@ColumnLocation>
@if (!string.IsNullOrWhiteSpace(Label))
{
<label for="@Id">@Label</label>
}
<InputText @bind-Value="@CurrentValueAsString" class="form-control" placeholder="@Label"></InputText>
<ValidationMessage For="@ValidationFor" />
</div>
@code {
[Parameter] public string Id { get; set; }
[Parameter] public string Label { get; set; }
[Parameter] public Expression<Func<string>> ValidationFor { get; set; }
protected override bool TryParseValueFromString(string value, out string result, out string validationErrorMessage)
{
decimal gg = decimal.Parse(value);
result = String.Format("{0:n2}", gg);
validationErrorMessage = null;
return true;
}
}
这是页面:
@page "/counter"
<EditForm Model="quote" OnValidSubmit="@SubmitButtonPressed" class="form-horizontal">
<DataAnnotationsValidator />
<UxInputText Label="Quote cost" @bind-Value="quote.TotalQuoteCost"
ValidationFor="@(() => quote.TotalQuoteCost)"
/>
<button type="submit" class="btn">Check 1</button>
<h1>@ttt</h1>
</EditForm>
@code {
private string ttt = string.Empty;
Quote quote = new Quote();
protected void SubmitButtonPressed()
{
ttt = quote.TotalQuoteCost.ToString();
}
}
如果我将 TotalQuoteCost 定义为小数(它必须是小数),我会收到以下错误:
1- CS1662 无法将 lambda 表达式转换为预期的委托类型,因为块中的某些返回类型不能隐式转换为委托返回类型
2- CS1503 参数 2:无法从“Microsoft.AspNetCore.Components.EventCallback”转换为“Microsoft.AspNetCore.Components.EventCallback”
3- CS0029 无法将类型“十进制”隐式转换为“字符串”
主要问题是您的组件继承了字符串类型的 InputBase 而不是十进制类型的 InputNumber:
@using System.Linq.Expressions
@inherits InputNumber<decimal>
<div class=@ColumnLocation>
@if (!string.IsNullOrWhiteSpace(Label))
{
<label for="@Id">@Label</label>
}
<InputText @bind-Value="@CurrentValueAsString" class="form-control" placeholder="@Label"/>
<ValidationMessage For="@ValidationFor" />
</div>
@code {
[Parameter] public string Id { get; set; }
[Parameter] public string Label { get; set; }
[Parameter] public Expression<Func<decimal>> ValidationFor { get; set; }
protected override bool TryParseValueFromString(string value, out decimal result, out string validationErrorMessage)
{
result = decimal.Parse(value);
validationErrorMessage = null;
return true;
}
}
然后你可以更新你的模型:
public class Quote
{
[Required]
public decimal TotalQuoteCost { get; set; }
}
除了小的编辑之外,我不确定我对页面进行了多少更改:
<EditForm Model="quote" OnValidSubmit="@SubmitButtonPressed" class="form-horizontal">
<DataAnnotationsValidator />
<UxInputText type="number" Label="Quote cost" @bind-Value="quote.TotalQuoteCost" ValidationFor="@(() => quote.TotalQuoteCost)" />
<button type="submit" class="btn">Check 1</button>
<h1>@ttt</h1>
</EditForm>
@code {
private string ttt = string.Empty;
Quote quote = new();
protected void SubmitButtonPressed()
{
ttt = quote.TotalQuoteCost.ToString();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.