简体   繁体   English

将当前URL插入@ Html.RouteLink参数以构建链接

[英]Insert current URL into @Html.RouteLink parameter to build a link

I am probably going about this the wrong way as i'm a noob to .NET MVC, but is there a way to insert the current url as a parameter into an @Html.Routelink? 我可能是错误的方式,因为我是.NET MVC的菜鸟,但是有没有办法将当前URL作为参数插入@ Html.Routelink?

I'm passing a partial view into a page to display a list of subcategories based off of the current category. 我将局部视图传递到页面中,以显示基于当前类别的子类别列表。

Here is my controller logic: 这是我的控制器逻辑:

   public PartialViewResult SubcategoryMenu(string category)
    {
        IEnumerable<string> subcategories = repository.Products
            .Where(x => x.SubCategory.Category.CategoryName == category)
            .Select(x => x.SubCategory.SubCategoryName)
            .Distinct()
            .OrderBy(x => x);

        return PartialView(subcategories);
    }

Here is my partial view which I'm trying to get the current category url into: 这是我的部分视图,我试图将当前类别的URL放入:

@model IEnumerable<string>
@foreach (var link in Model)
    {
        @Html.RouteLink(link, new
            {
                controller = "Product",
                action = "List",
                category = "INSERT CURRENT URL HERE",
                subcategory = link,
                page = 1
             }, new { @class = "btn btn-block btn-default btn-lg" })
     }

When the page displays, all products and categories are listed, no problem. 显示页面时,将列出所有产品和类别,没有问题。 When I click a Category, the URL is http://localhost/Category2 which is what I want. 当我单击类别时,URL是http://localhost/Category2 ,这就是我想要的。

Click a category, then all of the associated subcategories display in a separate div based on the linq query in the controller just fine. 单击一个类别,然后根据控制器中的linq查询,将所有关联的子类别显示在单独的div中。

However, to properly display the products, the url generated for the subcategory view needs to be http://localhost/Category/Subcategory and no matter how I tweek the @Html.RouteLink or even an @Html.Actionlink, all I can get is http://localhost/Subcategory If i pass in text into the @Html.Routelink for controller= "something" that will display http://localhost/something/Subcategory so I've been trying to get the current URL which matches the required category passed into the subcategory @Html.Routelink with no success. 但是,要正确显示产品,为子类别视图生成的url必须为http://localhost/Category/Subcategory ,无论我如何使用@ Html.RouteLink甚至是@ Html.Actionlink,我都能得到是http://localhost/Subcategory如果我在@ Html.Routelink中将文本传递给controller =“ something”,它将显示http://localhost/something/Subcategory所以我一直在尝试获取与之匹配的当前URL必需的类别已成功传递到子类别@ Html.Routelink。

If there is a way to insert the current URL into the subcategory RouteLink it would solve my woes. 如果有一种方法可以将当前URL插入到子类别RouteLink中,它将解决我的麻烦。 If there is a way to accomplish, or a better way to do so, please help. 如果有实现的方法,或者有更好的方法,请提供帮助。

Thank you all! 谢谢你们!

RouteLink and ActionLink use the RouteTable to build outgoing URLs, so it is the RouteTable you need to modify to customize URLs, not the RouteLink configuration. RouteLinkActionLink使用RouteTable来构建传出URL,因此需要修改以自定义URL的RouteTable ,而不是RouteLink配置。

You can do this many ways, but the most flexible way to make URLs that are driven off of database data is to subclass RouteBase . 您可以通过多种方式执行此操作,但是使脱离数据库数据的URL的最灵活的方法是RouteBase

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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