Jurioli / Blazor.WebForm.Components

ASP.NET Web Forms System.Web.UI.WebControls Razor Components For Blazor WebAssembly, Blazor Hybrid, Blazor Server.
MIT License
44 stars 9 forks source link

For ListView: The datasource keys["Id"] for OnExecuteUpdated method is lower than the actual Id #15

Closed MG1376 closed 5 months ago

MG1376 commented 5 months ago

I faced with this issue when I want to edit my listview items, But the update is wrong. the Id I receive in the datasource updated method should be 2454 (the editing row) but is 2453. this causes wrong update.

Jurioli commented 5 months ago

Can you provide code that reproduces this error?

MG1376 commented 5 months ago
<ItemTemplate TItem="GreenPaperItem" Context="item">     
     <div class="@cls row d-flex flex-row align-items-center p-1">
         <div class="col-md-3">
             <asp.Button Text="Delete" CommandName="Delete" CssClass="btn btn-danger">

             </asp.Button>
             <asp.Button Text="Edit" CommandName="Edit"   CssClass="btn btn-primary">

             </asp.Button>
         </div>
<asp.FreeDataSource ID="freesrc" OnExecuteSelected="this.Selected"
                     OnExecuteUpdated="this.Updated" OnExecuteInserted="this.Inserted"
                     OnExecuteDeletedAsync="this.Deleted" PaginationControlID="Pagination1"
                     @ref="fds">

 </asp.FreeDataSource>
protected void Updated(object sender, Coll.IDictionary keys, Coll.IDictionary values, Coll.IDictionary oldValues)
    {
        msg += "Datasource Updated, ";
        // Update record.
        var item = context.GreenPaperItems.Where(a => a.Id == (int)keys["Id"])
            .Apply(a => values);
        item.First().Date = new DateTime(addingDate.Year, addingDate.Month, addingDate.Day,
AddingTime.HasValue ? AddingTime.Value.Hour : 0,
AddingTime.HasValue ? AddingTime.Value.Minute : 0, 0);
        context.SaveChanges();
        this.RequestRefresh();
    }
protected IQueryable<GreenPaperItem> Selected(object sender)
{
    msg += "Datasource Selected, ";
    InvokeAsync(() => StateHasChanged());
    var query = context.GreenPaperItems.AsQueryable();

    if (df == DateFilter.YearMonth && filterYear != null && filterMonth != null)
    {
        (DateTime start, DateTime end) =
           Business.Home.GetGregorianStartAndEndForPersianYearAndMonth(filterYear.Value, filterMonth.Value);
        query = query.Where(w => w.Date.Date >= start.Date && w.Date.Date <= end.Date);
    }
    else if (df == DateFilter.Default)
    {
        PersianCalendar pc = new PersianCalendar();
        int pYear = pc.GetYear(DateTime.Now);
        int pMonth = pc.GetMonth(DateTime.Now);
        int daysInMonth = pc.GetDaysInMonth(pYear, pMonth);
        DateTime start = pc.ToDateTime(pYear, pMonth, 1, 12, 0, 0, 0);
        DateTime end = start.AddDays(365); // daysInMonth);
        mf = (MonthFilter)Enum
    .Parse(typeof(MonthFilter), ddlMonthFilter.SelectedValue);
        DateTime nowMinusOneSecond = DateTime.Now.Date.AddSeconds(-1);
        query = (mf) switch
        {
            MonthFilter.After =>
               query.Where(it => it.Date >= nowMinusOneSecond && it.Date <= end.Date),
            MonthFilter.All =>
               query.Where(it => it.Date >= start.Date && it.Date <= end.Date),
            MonthFilter.Before =>
               query.Where(it => it.Date >= start.Date && it.Date < nowMinusOneSecond)
        };
    }

    if (!string.IsNullOrWhiteSpace(currSortExpression))
    {
        // Sort via the OrderBy method
        query = query.OrderBy($"{currSortExpression} {currSortDirecton.ToString()}");
    }
    else
    {
        query = query.OrderBy(w => w.Date);
    }

    //Trace.WriteLine("Weather SQL:");
    //Trace.WriteLine(query.ToQueryString());

    //count = query.Count();

    // Perform paginv via Skip and Take.
    return query; //.Skip(pager.StartRowIndex).Take(query.Count()).ToList<GreenPaperItem>();
}
MG1376 commented 5 months ago

It is working, I just added a HtmlSubmitButton programmaticaly instead of regular Update button so that I can put a RadzenTemplateForm in the template to validate radzen components, Although it was good but after removing it everything works fine. I updated the listview using UpdateItem and InsertNewItem methods of the listview, they may still be wrong and need consideration.

MG1376 commented 5 months ago

I tested more it still has the problem with the default conditions.

Jurioli commented 5 months ago

I can't reproduce the issue like this code Already tried to test the Example combination of ListView and DataPager, CustomerID no issue

I don’t know if it has anything to do with the following, it’s not the same as your issue description.

Have you tried adding SubstituteItem="false" to EditItemTemplate? SubstituteItem is set to create an additional Item during editing to isolate changes to the queried Item, allowing it to be restored when editing is canceled.

<EditItemTemplate SubstituteItem="false" TItem="GreenPaperItem" Context="item">

But this makes cancellation look like it has no effect.

MG1376 commented 5 months ago

I think the error is from some date conversions, and yes I set the context="item" exactly as you said.