Question

I am using ASP MVC 5 and trying to generate a link in my view inside the "ControlPanel" Area

my view rootfolder/Areas/ControlPanel/Views/CourseCategory/Index.cshtml

@Html.ActionLink("edit", "Edit", new { id = item.Id }, new { @class = "btn btn-default btn-xs" })
@Html.ActionLink("delete", "Delete", new { id = item.Id }, new { @class = "btn btn-danger btn-xs" })

my AreaRegistration

public class ControlPanelAreaRegistration : AreaRegistration 
    {
        public override string AreaName 
        {
            get 
            {
                return "ControlPanel";
            }
        }

        public override void RegisterArea(AreaRegistrationContext context) 
        {
            context.Routes.LowercaseUrls = true;

            context.MapRoute(
                name: "ControlPanel.CourseCategory",
                url: "controlpanel/course-categories/{id}",
                defaults: new { controller = "CourseCategory", action = "Index", id = UrlParameter.Optional },
                namespaces: new[] { "Website.Areas.ControlPanel.Controllers" }
            );
        }
    }

RouteConfig file

public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.LowercaseUrls = true;

            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "HomePage",
                url: "",
                defaults: new { controller = "Home", action = "Index" }
            );

        }
    }

my controller

namespace Website.Areas.ControlPanel.Controllers
{
    public class CourseCategoryController : ControlPanelController
    {

        public CourseCategoryController(IUnitOfWork uow)
            :base(uow)
        {
        }

        public ActionResult Index()
        {
            return View(_uow.Repository<CourseCategory>().GetAllOrderByCode());
        }
    }
}

now the output html produces empty href

<a class="btn btn-default btn-xs" href="">edit</a>
<a class="btn btn-danger btn-xs" href="">delete</a>

what is the correct way of generating link on Html.ActionLink or Url.Action and Url.RouteUrl in this case?

Était-ce utile?

La solution

It looks like you are missing the controller in your route values (I noticed you are not specifying one anywhere in your @Html.ActionLink)

@Html.ActionLink("edit", "Edit", new { controller="CourseCategory", id = item.Id }, new { @class = "btn btn-default btn-xs" })

Or - an alternative overload of the Html.ActionLink

@Html.ActionLink("edit", "Edit", "CourseCategory", new { id = item.Id }, new { @class = "btn btn-default btn-xs" })

Lastly, it never hurts to include the area name as part of the RouteValues dictionary, but this is not required.

EDIT

It looks like you updated your question with some additional information. This does not look right (having a blank value for URL)

routes.MapRoute(
  name: "HomePage",
  url: "",
  defaults: new { controller = "Home", action = "Index" }
);

By default, it should look something like this

routes.MapRoute(
  name: "HomePage",
  url: "{controller}/{action}",
  defaults: new { controller = "Home", action = "Index" }
);

I would assume having a blank URL is definitely something that could cause a blank URL to be created in an Html.ActionLink. These helpers looks at your route config to generate the URL and this one seems to be taking over probably everything since it matches everything.

Autres conseils

Try to do this area name along with id

@Html.ActionLink("edit", "Edit", new { id = item.Id, area = "ControlPanel" }, new { @class = "btn btn-default btn-xs" })
@Html.ActionLink("delete", "Delete", new { id = item.Id, area = "ControlPanel" }, new { @class = "btn btn-danger btn-xs" })

I think the problem is with the defaults which points to the Index action and action is not bound on the route!??

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top