1

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??

2
  • Possible duplicate of Url.RouteUrl returns null Commented Sep 8, 2016 at 6:18
  • what should i do??how to solve??sir the error is on Url word..it shows doesnot exist Commented Sep 8, 2016 at 6:19

1 Answer 1

1

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>
     }
 }
Sign up to request clarification or add additional context in comments.

2 Comments

@ManojMaharana - I have edited the post - hope it helps.
sir the error comes near UrlHelper,Controller,Controller. the type of namespace could not found...sir what should i do??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.