簡體   English   中英

在自定義 InputDate 組件中設置 dd/mm/yyyy 而不是 Blazor 中的 01/01/0001

[英]Set dd/mm/yyyy in a custom InputDate component instead of 01/01/0001 in Blazor

我在 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<System.DateTime?>”轉換為“Microsoft.AspNetCore.Components.EventCallback”

無法將 lambda 表達式轉換為預期的委托類型,因為塊中的某些返回類型不能隱式轉換為委托返回類型

無法隱式轉換類型“System.DateTime?” 到'System.DateTime'。 存在顯式轉換(您是否缺少演員表?)

您將InputBase包裝在一個有點復雜的InputBase中。 使用InputBase將任何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>

如果你想要一個更復雜的解決方案,這里是我的一個屏幕截圖。

在此處輸入圖片說明 我可以指向你的回購代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM