Search code examples
mudblazor

mudblazor - daterange picker


This is my code for the dateranger picker.


<MudDateRangePicker Label="Date Ranges" @bind-DateRange="callfunction(record)" />

private DateRange callfunction(SEQuestionnaire record)
    {
        return new DateRange(record.DataCalendar1, record.DataCalendar2);
    }

But I get an error saying The left-hand side of an assignment must be a variable, property or indexer 331 217

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 332 217


Solution

  • You can't add a @bind directive on a function. Use the DateRange property and DateRangeChanged EventCallback for custom logic.

    Make sure your SEQuestionnaire.DataCalendar1 are of type, nullable DateTime?

    <MudDateRangePicker Label="Basic range picker" DateRange="_dateRange" DateRangeChanged="(e)=>HandleDateRangeChange(e,_record)" />
    
    @code { 
        private MyRecord _record ;
        private DateRange _dateRange;
        List<string> passedChange = new();
        protected override void OnInitialized()
        {
            _record = new (){dt1=DateTime.Now.Date, dt2=DateTime.Now.AddDays(5).Date};
            _dateRange =  new DateRange(_record.dt1,_record.dt2);
        }
        void HandleDateRangeChange(DateRange newDateRange,MyRecord passedRecord)
        {
            // do something with passedRecord
            passedChange.Add(passedRecord.dt1.ToString());
    
            // or change the existing record
            _record.dt1 = newDateRange.Start;
            _record.dt2 = newDateRange.End;
    
            // make sure to change the original field that is on DateRange
            _dateRange = newDateRange;
        }
        public class MyRecord{
            public DateTime? dt1 {get;set;}
            public DateTime? dt2 {get;set;}
        }
    }