0

My controller doesn't come back to list after deleting an item from the list, in browser it throws this error

This localhost page can’t be found. No web page was found for the web address: https://localhost:44384/RaidRequests/IndexAsync

This started then I added my pagination, i took care of all other problems with my views, but delete just doesn't work.

this is my delete function in my controller:

public async Task<IActionResult> DeleteConfirmed(int? id)
    {
        var raidRequest = await _context.RaidRequest.FindAsync(id);
        _context.RaidRequest.Remove(raidRequest);
        await _context.SaveChangesAsync();
        return RedirectToAction(nameof(IndexAsync));
    }

and my index looks like this:

public async Task<ViewResult> IndexAsync(string sortOrder, string currentFilter, string searchString, int? page)
    {
        ViewBag.CurrentSort = sortOrder;
        ViewBag.NameSurnameSortParm = sortOrder == "NameSurname" ? "NameSurname_desc" : "NameSurname";
        ViewBag.ReasonSortParm = sortOrder == "Reason" ? "Reason_desc" : "Reason";
        ViewBag.AccessSortParm = sortOrder == "Access" ? "Access_desc" : "Access";
        ViewBag.UserOrAdminSortParm = sortOrder == "UserOrAdmin" ? "UserOrAdmin_desc" : "UserOrAdmin";
        ViewBag.DepartmentSortParm = sortOrder == "Department" ? "Department_desc" : "Department";
        ViewBag.UNCPathSortParm = sortOrder == "UNCPath" ? "UNCPath_desc" : "UNCPath";

        if (searchString != null)
        {
            page = 1;
        }
        else
        {
            searchString = currentFilter;
        }

        ViewBag.CurrentFilter = searchString;

        var request = from c in _context.RaidRequest
                      select c;
        if (!String.IsNullOrEmpty(searchString))
        {
            request = request.Where(s => s.NameSurname.Contains(searchString));
        }
        request = sortOrder switch
        {
            "NameSurname_desc" => request.OrderByDescending(c => c.NameSurname),
            "Reason" => request.OrderBy(c => c.Reason),
            "Reason_desc" => request.OrderByDescending(c => c.Reason),
            "Access" => request.OrderBy(c => c.Access),
            "Access_desc" => request.OrderByDescending(c => c.Access),
            "UserOrAdmin" => request.OrderBy(c => c.UserOrAdmin),
            "UserOrAdmin_desc" => request.OrderByDescending(c => c.UserOrAdmin),
            "Department" => request.OrderBy(c => c.Department),
            "Department_desc" => request.OrderByDescending(c => c.Department),
            "UNCPath" => request.OrderBy(c => c.UNCPath),
            "UNCPath_desc" => request.OrderByDescending(c => c.UNCPath),
            _ => request.OrderBy(c => c.Id),
        };
        int pageSize = 10;
        int pageNumber = (page ?? 1);
        return View(await PaginatedList<RaidRequest>.CreateAsync(request, page ?? 1, pageSize)); 
    }

I think the problem is with the return part. What do I need to write there for it to go back to the list successfully?

1 Answer 1

2

The action name is Index. The error complains that the URL is :

https://localhost:44384/RaidRequests/IndexAsync

instead of

https://localhost:44384/RaidRequests/Index

That's because nameof(IndexAsync) returns IndexAsync instead of Index. The return statement should be :

return RedirectToAction("Index");
Sign up to request clarification or add additional context in comments.

2 Comments

Can you please explain how did you find that the action name is Index? I am asking this because I didn't see any attribute over the IndexAsync method, neither any method calling Index in the OP's question. I just found that OP mentioned my index looks like this but it could be an abbreviation for IndexAsync as well. I am just curious, am I missing something?
This worked, i think when i tried this i just forgot to add "" to index. thanks, now i feel a bit stupid :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.