Vy Nguyen
Vy Nguyen

Reputation: 57

Route to subfolder in ASP.NET Core MVC

I want to route to the Edit page in a subfolder within my Companies model's view (Views/Companies/Employees/Edit).

I tried to do it using <a asp-action="Employees/Edit" asp-route-id="@model.ID">Edit</a> in the Index.cshtml of the Companies model's view which opens a blank page with the link http://localhost:xxxxx/Companies/Employees%2FEdit/1. The correct view should be on http://localhost:xxxxx/Companies/Employees/Edit/1.

Can anyone explain to me why I'm getting this?

This is the Startup.cs:

app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });

Upvotes: 1

Views: 2308

Answers (1)

scartag
scartag

Reputation: 17680

You haven't set the asp-controller attribute.

<a asp-action="Employees/Edit" asp-route-id="@model.ID">Edit</a>

should be

<a asp-action="Edit" asp-controller="Employees" asp-route-id="@model.ID">Edit</a>

I'm not sure why you have the sub folders the way you do. It's supposed to be Views/ControllerName/ActionName (that's the convention)

Do you have an Edit action in the Employees Controller? or an Edit action in the Companies controller?

Upvotes: 4

Related Questions