Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find Area name and Controller Name in custom Htmlhelper with ASP.NET MVC3

I try to rewrite and customize @Html.ActionLink, in one of overloads of this method the parameters are:

public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, 
                                       string linkText,   string actionName);

And I want something like the above and also need to find AreaName and ControllerName without pass it by parameters, I think to use the followings:

string controller = ViewContext.RouteData.Values["Controller"];
string area = ViewContext.RouteData.DataTokens["Area"];

but the error rise as :

An object reference is required for the non-static field, method, or property
'System.Web.Mvc.ControllerContext.RouteData.get'

And obviously I use static, so what is your suggestion to find Area Name and Controller Name in HtmlHelpers ?

like image 556
Saeid Avatar asked Mar 17 '12 09:03

Saeid


1 Answers

Use this:

string controllerName = 
(string)htmlHelper.ViewContext.RouteData.GetRequiredString("controller");

string areaName = 
(string)htmlHelper.ViewContext.RouteData.DataTokens["area"];
like image 97
Saeid Avatar answered Sep 21 '22 16:09

Saeid