Search code examples
c#asp.net-corerazor-pages

An unhandled exception occurred while processing the request InvalidOperationException: The views was not found


I get this error when I press my "edit" and "delete" buttons. In short, I am in the process of developing a website where administrators can see their created users and make changes.

<td><a asp-controller="Admin" class="btn btn-sm btn-primary" asp-action="Edit" asp-route-id="@item.record_id">Edit</a></td>
<td><a asp-controller="Admin" class="btn btn-sm btn-danger" asp-action="Delete" asp-route-id="@item.record_id">Delete</a></td>
          

The error message I am getting after the button is pressed:

An unhandled exception occurred while processing the request.
InvalidOperationException: The view 'Edit' was not found. The following locations were searched:

/Views/Admin/Edit.cshtml
/Views/Shared/Edit.cshtml
/Pages/Shared/Edit.cshtml

Microsoft.AspNetCore.Mvc.ViewEngines.ViewEngineResult.EnsureSuccessful(IEnumerable<string> originalLocations)

This error occurs for both buttons and gives exactly the same error. I have read that this error is because there are multiple locations for the file. But there isn't and I don't understand that. What is the cause of my error?

Here's an image of my project strucutre: enter image description here


Solution

  • The view 'Edit' was not found. The following locations were searched:

    /Views/Admin/Edit.cshtml
    /Views/Shared/Edit.cshtml
    /Pages/Shared/Edit.cshtml

    The error means that the framework could not find a view file (.cshtml file) named Edit in any of the locations listed. These are the default locations for view templates in ASP.NET Core MVC. The first is in a folder in the Views directory named after the controller, and the other two are general locations where you would typically place view files that might be used by more than one controller action (such as a Layout file).

    The view file itself should be named after the controller's action method (i.e. Edit or Delete in your case). Rename the Pages folder to Views and put your view files in an Admin folder within that:

    > Views
        > Admin
            Edit.cshtml
            Delete.cshtml