首页 > 解决方案 > 无法 MudDatePicker 如何将其绑定到数据模型

问题描述

<EditForm Model="@_newRegister" OnValidSubmit="@HandleValidSubmit">
  <DataAnnotationsValidator />
  <ValidationSummary />
  <div class="container-fluid">
    <div class="row">
     <div class="col-6">
      <div class="form-group">
        <MudDatePicker Label="Collection Date" Editable="true" @bind-Date="_newRegister.CollectionDate" />
      </div>
     </div>
    </div>
  </div>
</Editform>

@code{
private Register _newRegister = new x.Shared.Register
{
    ProcessingDate = DateTime.Today,
    CollectionDate = DateTime.Today,
    ModifiedDate = DateTime.Today,
    CreateDate = DateTime.Today
};

private string Success = "";
DateTime? date = DateTime.Today;

public void HandleValidSubmit()
{

    Success = "Success";

}
)

public class Register
{
    [Key]
    public int ID { get; set; }
    public String CustomerName { get; set; }
    public DateTime CollectionDate { get; set; }
    public DateTime ProcessingDate { get; set; }
    public String Location { get; set; }
    public String Remarks { get; set; }
    public String Reference { get; set; }
    public int Type { get; set; }

}

'''

我收到此错误..如果我使用本地日期时间变量(日期),则此错误消失了,但是我无法再将其映射到模型..任何解决方案如何在此上绑定模型

错误 CS1662 无法将 lambda 表达式转换为预期的委托类型,因为块中的某些返回类型不能隐式转换为委托返回类型 X.Client

错误 CS1503 参数 2:无法从 'Microsoft.AspNetCore.Components.EventCallback<System.DateTime>' 转换为 'Microsoft.AspNetCore.Components.EventCallback' x.Client D:\x\Client\obj\Debug\net5.0\ Razor\Pages\CashRegister\AddRegister.razor.g.cs 174 活动

错误 CS1503 参数 2:无法从 'Microsoft.AspNetCore.Components.EventCallback<System.DateTime>' 转换为 'Microsoft.AspNetCore.Components.EventCallback' D:\x\Client\Pages\CashRegister\AddRegister.razor
错误 CS1662 无法转换lambda 表达式到预期的委托类型,因为块中的某些返回类型不能隐式转换为委托返回类型 D:\x\Client\Pages\CashRegister\AddRegister.razor

标签: c#.net-5blazor-webassembly

解决方案


我知道我迟到了大约 8 个月,但答案是你需要绑定到一个可以为空的DateTime.

所以你的财产需要public DateTime? CollectionDate { get; set; },它应该工作。我只是有同样的问题。

他们的API也说

注意:总是使用双向绑定 @bind-Date 绑定到 DateTime 类型的字段?

请参阅:https ://resize-listener-mudblazor-test.azurewebsites.net/components/datepicker#basic-usage


推荐阅读