0

我正在使用 ASP MVC 5 并尝试在“ControlPanel”区域内的视图中生成链接

我的视图 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" })

我的区域注册

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

路由配置文件

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

        }
    }

我的控制器

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

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

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

现在输出html产生空href

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

在这种情况下,在 Html.ActionLink 或 Url.Action 和 Url.RouteUrl 上生成链接的正确方法是什么?

4

3 回答 3

1

看起来您在路由值中缺少控制器(我注意到您没有在您的任何地方指定一个@Html.ActionLink

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

或者 - 的替代重载Html.ActionLink

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

最后,将区域名称作为 RouteValues 字典的一部分包含在内永远不会有什么坏处,但这不是必需的。

编辑

您似乎用一些附加信息更新了您的问题。这看起来不正确(URL 有一个空白值)

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

默认情况下,它应该看起来像这样

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

我会假设有一个空白 URL 肯定会导致在Html.ActionLink. 这些助手查看您的路由配置以生成 URL,而这个似乎正在接管所有内容,因为它匹配所有内容。

于 2014-01-26T06:24:34.513 回答
0

尝试将此区域名称与 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" })
于 2014-01-26T06:47:47.993 回答
0

我认为问题在于指向索引操作的默认值,并且操作未绑定在路由上!??

于 2014-01-26T06:55:31.070 回答