Arun Kat
Arun Kat

Reputation: 71

Unable to MudDatePicker how to bind it to a Datamodel

<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; }

}

'''

i am getting this error .. if i use a local datetime variable (date) this error is gone, however i am not able to mapp that to the model anymore .. Any solution how to do i bind the model on this

Error CS1662 Cannot convert lambda expression to intended delegate type because some of the return types in the block are not implicitly convertible to the delegate return type X.Client

Error CS1503 Argument 2: cannot convert from 'Microsoft.AspNetCore.Components.EventCallback<System.DateTime>' to 'Microsoft.AspNetCore.Components.EventCallback' x.Client D:\x\Client\obj\Debug\net5.0\Razor\Pages\CashRegister\AddRegister.razor.g.cs 174 Active

Error CS1503 Argument 2: cannot convert from 'Microsoft.AspNetCore.Components.EventCallback<System.DateTime>' to 'Microsoft.AspNetCore.Components.EventCallback' D:\x\Client\Pages\CashRegister\AddRegister.razor
Error CS1662 Cannot convert lambda expression to intended delegate type because some of the return types in the block are not implicitly convertible to the delegate return type D:\x\Client\Pages\CashRegister\AddRegister.razor

Upvotes: 7

Views: 1739

Answers (1)

ISeeSharp
ISeeSharp

Reputation: 395

I know I'm late to the party by about 8 months, but the answer is you need to bind to a nullable DateTime.

So your property needs to be public DateTime? CollectionDate { get; set; } and it should work. I just had the same problem.

Also their API says

Note: Always use the two-way binding @bind-Date to bind to a field of type DateTime?

See: https://www.mudblazor.com/components/datepicker#basic-usage

Upvotes: 6

Related Questions