Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, everyone.
I'm facing pretty strange behavior in my Blazor hosted app. Controller correctly receives parameters from client service and then invokes method in my repository for retreiving data. But problem is when invokes method in repository, parameters are changed. pageIndex and pageSize parameters are set to empty string although they are integer type. I've tried to remove code that filters allCategories by searchText, and just return new Pagintion<categorydto>, and everithing works fine. Thank you for help in advance.

What I have tried:

// GET method for returning paginated list of CategoryDto objects
        [HttpGet]
        public IActionResult GetCategories(string? searchText, int pageIndex, int pageSize)
        {
            // Call CategoryRepository's method for returning paginated list of CategoryDto objects
            var pagination = unitOfWork.CategoryRepository.GetFilteredCategories(searchText ?? string.Empty, pageIndex, pageSize);

            // Return status code 200 along with list of CategoryDto objects
            return Ok(pagination);
        }


// CategoryRepository method

public Pagination<CategoryDto> GetFilteredCategories(string searchText, int pageIndex, int pageSize)
        {
            var allCategories = context.Categories.AsQueryable();

            if (!string.IsNullOrEmpty(searchText))
            {
                allCategories = allCategories.Where(e => e.Code.ToLower().Contains(searchText.ToLower())
                                                || e.Name.ToLower().Contains(searchText.ToLower()));
            }

            //Variable that will contain CategoryDto objects
            List<CategoryDto> categoryDtos = new();

            // Iterate through allCategories and populate categoryDtos
            // using Category's extension method ConvertToDto()
            foreach (var category in allCategories)
            {
                categoryDtos.Add(category.ConvertToDto());
            }

            // Using PaginationUtility's GetPaginatedResult() method, 
            // return Pagionation<CategoryDto> object
            return PaginationUtility<CategoryDto>.GetPaginatedResult(in categoryDtos, pageIndex, pageSize);
        }
Posted
Comments
Pete O'Hanlon 27-Nov-23 2:59am    
When you put a breakpoint in GetCategories, what values do the parameters contain?
George Swan 27-Nov-23 9:59am    
I think you are trying to implement Blazor as if it was a request/response based traditional web application. That is not the case. My suggestion is to look for examples of Blazor’s state management, component use and navigation techniques.
Graeme_Grant 27-Nov-23 15:59pm    
Above is server-side code, has nothing to do with Blazor itself. The [HttpGet] should have given it away! ;)
George Swan 28-Nov-23 4:13am    
Graeme, I couldn't agree with you more. The OP refers to 'my Blazor hosted app'. My suggestions were aimed at enabling the OP to understand that there is no need to expose APIs from a Blazor environment. They did not arise due to an ignorance of Http methods. Best wishes, George.
Graeme_Grant 28-Nov-23 14:30pm    
To me, looking at the code posted, it looked like Web Assembly hosted in ASP.NET Core with a web API controller. If I recall correctly, the File > New Project, prior to VS v17.8 (.Net 8.0) used the word "hosted" for an option for Blazor WASM. I could be wrong...

I would use minimal APIs with WASM, much cleaner.

1 solution

A few things:
1. You're asking for paging however paging is not part of the EF query.
2. An EF GET should use NoTracking unless you are going to update and postback to the db. In your case, you're running a pagination query and forget with Blazor, so no tracking is required.
3. Why are you using a UnitOfWork class for a GET query??? A UnitOfWork class usually wraps operations for a Transaction for a complex Create/Update/Delete operations across multiple operations on a datsabase, not a single GET.
4. You don't need to iterate over the EF collection, you can convert as part of the Linq query. It will be quicker.
C#
List<CategoryDto> categoryDtos = allCategories
    .Where(e => e.Code.ToLower().Contains(searchText.ToLower())
                || e.Name.ToLower().Contains(searchText.ToLower()))
    .Select(x => x.ConvertToDto()
    .ToList();

Quote:
parameters are changed. pageIndex and pageSize parameters are set to empty string although they are integer type.

Are you talking in your razor page? Both the razor page and PaginationUtility class code are missing from your question, so we can't see how this is happening. Please click on the green Improve question link and update your question
 
Share this answer
 
v2
Comments
Gruja82 28-Nov-23 0:56am    
Thank for your answer. Thing is that only when I put a breakpoint, debugger shows string values in integer parameters. Everything works good. I'm sorry.
To answer to your comments:
1. Paging is done by invoking static method GetPaginatedResult(...) which calculates number of pages
based on input parameters.
2. I know that. I am using it in other entities that have relations with other entities, just forgot
to use it here.
3. UnitOfWork class wraps up all my specific repositories which in turn define all operations that can be applied to specific entity.
4. Yes you are wright, but if I am not mistaken, I am iterating over a IQueryable<t> collection which does not contain actual data, but query. GetPaginatedResult static method returns actual data.
If you like you can take a look at my GitHub repository: https://github.com/Gruja82/BlazorCompany.
Thank you again for your effort and again sorry.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900