我在blazor中有一个自定义的InputDate控件,它工作得很好,但当我运行pp时,我得到的是01/01/0001,而不是dd/mm/yyyy。
如果不将时间设置为NOW,我想设置dd/mm/yyyy
如果我像往常一样使用InputDate,它可以正常工作,并显示dd/mm/yyyy,但在自定义控件中,我得到一些错误
这是我的模型:
public class Quote
{
[Required(ErrorMessage = "Quote Date is a required field.")]
public DateTime? QuoteDate { get; set; }
}
和海关管制:
@using System.Linq.Expressions
@inherits InputBase<DateTime>
<div class=@ColumnLocation>
@if (!string.IsNullOrWhiteSpace(Label))
{
<label for="@Id">@Label</label>
}
<InputDate @bind-Value="@CurrentValue" class="form-control" placeholder="Enter date of preference"></InputDate>
<ValidationMessage For="@ValidationFor" />
</div>
@code {
[Parameter] public string Id { get; set; }
[Parameter] public string Label { get; set; }
[Parameter] public Expression<Func<DateTime>> ValidationFor { get; set; }
protected override bool TryParseValueFromString(string value, out DateTime result, out string validationErrorMessage)
{
result = DateTime.Parse(value);
validationErrorMessage = null;
return true;
}
}
和页面:
@page "/counter"
<EditForm Model="quote" OnValidSubmit="@SubmitButtonPressed" class="form-horizontal">
<DataAnnotationsValidator />
@*for this custom control I've had some errors*@
<UxInputDate Label="Quote Date3" @bind-Value="quote.QuoteDate"
ValidationFor="@(() => quote.QuoteDate)"/>
@*for this normal control everything is fine*@
<InputDate @bind-Value="quote.QuoteDate" class="form-control" ></InputDate>
<button type="submit" class="btn">Check 1</button>
</EditForm>
@code {
Quote quote = new Quote();
protected void SubmitButtonPressed()
{
....
}
}
错误:
无法从'Microsoft.AspNetCore.Components.EventCallback‘转换为'Microsoft.AspNetCore.Components.EventCallback’
无法将lambda表达式转换为所需的委托类型,因为块中的某些返回类型不能隐式转换为委托返回类型
无法隐式转换类型“”System.DateTime?“”设置为“System.DateTime”。存在显式转换(是否缺少强制转换?)
发布于 2021-11-03 22:04:25
您正在将一个InputBase
包装在一个InputBase
中,这有点复杂。使用passthrough将任何InputBase
控件包装在另一个组件中所需的代码比您已实现的代码要多一点。有一种更简单的方法可以为带有标签和验证消息的输入创建通用包装器控件。
@using System.Linq.Expressions
@typeparam TValue
<div class="m-2 p-2">
@if (!string.IsNullOrWhiteSpace(Label))
{
<label for="@Id">@Label</label>
}
@ChildContent
<ValidationMessage For="@ValidationFor" />
</div>
@code {
[Parameter] public string Id { get; set; }
[Parameter] public string Label { get; set; }
[Parameter] public Expression<Func<TValue>> ValidationFor { get; set; }
[Parameter] public RenderFragment ChildContent { get; set; }
}
然后像这样实现:
<CompositeInputControl Label="String" ValidationFor="() => _model.SelectValue">
<input @bind-value="_model.SelectValue" />
</CompositeInputControl>
<CompositeInputControl Label="Date" ValidationFor="() => _model.Time">
<InputDate @bind-Value="_model.Time"></InputDate>
</CompositeInputControl>
如果你想要一个更复杂的解决方案,这里有我的一个屏幕截图。
我可以告诉你回购代码的位置。
https://stackoverflow.com/questions/69811444
复制相似问题