Manoj Maharana
Manoj Maharana

Reputation: 145

@Url.RouteUrl does not exist in the current context in asp.net mvc4

I get error on url(<a href="@Url.RouteUrl....). The error is on the Url word.. Error is the url does not exist in the current context it works in another view page

@helper GetTreeView(Abacus_CMS.Models.AbacusModel siteMenu, int parentID)
{
   foreach (var i in siteMenu.AbacusMenuList.Where(a => a.ParentCatagoryId.Equals(parentID)))
   {
    <li>
        @{ var submenu = siteMenu.AbacusMenuList.Where(a => a.ParentCatagoryId.Equals(i.Id)).Count();}
        @if (submenu > 0)
        {
            <li style="margin-left: -6px;">
                <a href="@Url.RouteUrl("AbacusPage", new { catname = HttpUtility.UrlEncode(i.Name.Replace(' ', '-'))})" id="@i.Name.Replace(' ', '-').ToLower()">@i.Name
                    <i class="icon-user"></i><span class="title" style="margin-left: -24px;">@i.Name</span>
                    <span class="arrow " style="height: 4px;"></span>
                </a>
                <ul class="sub-menu">
                    @treeview.GetTreeView(siteMenu, i.Id)
                    @* Recursive  Call for Populate Sub items here*@
                </ul>
            </li>
        @*<span class="collapse collapsible">&nbsp;</span>*@
        }
        else
        {
            <a href="@Url.RouteUrl("AbacusPage", new { catname = HttpUtility.UrlEncode(i.Name.Replace(' ', '-')), style="margin-left: 30px;"})" id="@i.Name.Replace(' ', '-').ToLower()">@i.Name
            </a>
        }
        </li>
     }
 }

how to solve??

Upvotes: 1

Views: 697

Answers (1)

Ralf B&#246;nning
Ralf B&#246;nning

Reputation: 15415

The static UrlHelper-Instance "Url" is not defined inside the context of a MVC helper. But you can get an own instance via the Html.ViewContext.Controller or by creating a instance with the help of the Html.ViewContext (described in the linked SO post).

You can do something like that

@helper MyHelper()
{
    UrlHelper url= ((Controller) Html.ViewContext.Controller).Url;

    <span>@url.RouteUrl(new {Controller = "Home", Action = "Action"})</span>
}

Some nice helper methods are given here: Generate URL in HTML helper

Applied to your code:

@helper GetTreeView(Abacus_CMS.Models.AbacusModel siteMenu, int parentID)
{
   UrlHelper url= ((Controller) Html.ViewContext.Controller).Url;
   foreach (var i in siteMenu.AbacusMenuList.Where(a => a.ParentCatagoryId.Equals(parentID)))
   {
    <li>
        @{ var submenu = siteMenu.AbacusMenuList.Where(a => a.ParentCatagoryId.Equals(i.Id)).Count();}
        @if (submenu > 0)
        {
            <li style="margin-left: -6px;">
                <a href="@url.RouteUrl("AbacusPage", new { catname = HttpUtility.UrlEncode(i.Name.Replace(' ', '-'))})" id="@i.Name.Replace(' ', '-').ToLower()">@i.Name
                    <i class="icon-user"></i><span class="title" style="margin-left: -24px;">@i.Name</span>
                    <span class="arrow " style="height: 4px;"></span>
                </a>
                <ul class="sub-menu">
                    @treeview.GetTreeView(siteMenu, i.Id)
                    @* Recursive  Call for Populate Sub items here*@
                </ul>
            </li>
        @*<span class="collapse collapsible">&nbsp;</span>*@
        }
        else
        {
            <a href="@url.RouteUrl("AbacusPage", new { catname = HttpUtility.UrlEncode(i.Name.Replace(' ', '-')), style="margin-left: 30px;"})" id="@i.Name.Replace(' ', '-').ToLower()">@i.Name
            </a>
        }
        </li>
     }
 }

Upvotes: 1

Related Questions