简体   繁体   中英

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?}");
            });

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM