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?
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:
@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.
Click a category, then all of the associated subcategories display in a separate div based on the linq query in the controller just fine.
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.
If there is a way to insert the current URL into the subcategory RouteLink it would solve my woes. 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.
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
.