Reputation: 4482
Is there a way to have action links which are relative to the current view.
For example, lets say I have a partial view which is a contains a paged list of news articles called _ArticlesList
. I want to include this in the Admin
and Index
views, which are controlled by their relative controllers. _ArticlesList
produces URLs which have the routeValues
pageNumber
and pageSize
, but you have to hard code the controller, don't you?
I think what I want to do is just override properties in the routeValue
object?
Edit:
I guess I could use HttpContext.Current.Request.RequestContext.RouteData.Values["controller"].ToString()
but that looks pretty bad
Upvotes: 1
Views: 40
Reputation: 4482
I used this to achieve what I wanted
public static class UrlHelperExtensions
{
public static string RelativeAction(this UrlHelper url, object routeValues)
{
var routeDataValues = url.RequestContext.RouteData.Values;
var queryString = url.RequestContext.HttpContext.Request.QueryString;
foreach (string key in queryString.Keys)
{
routeDataValues[key] = queryString[key];
}
// Allow routeValue object to take precedence over queryString
foreach (var prop in new RouteValueDictionary(routeValues))
{
routeDataValues[prop.Key] = prop.Value;
}
return url.RouteUrl(routeDataValues);
}
}
Upvotes: 0
Reputation: 124
If you want to upload in For EX. Index file then write following to implement in your cshtml.
<div>
@Html.Partial("_ArticleList", Model)
</div>
You can put this in any div tag.
Upvotes: 1