asp.net-mvcasp.net-mvc-4razorhtml-helperrazor-2

How to use Url.Action in Razor view in App_Code folder


How to use Url.Action() in Razor helper from App_Code folder ?

I tried according to Why I cant use Html.RenderPartial in razor helper view File in App_Code Folder?

@using System.Web.Mvc.Html
@helper Tabel(System.Web.Mvc.HtmlHelper html)
{ 
    @Html.Raw(Url.Action("Index", "Home"))
}

but got compile error

CS0103: The name 'Url' does not exist in the current context

ASP.NET MVC4 and jquery are used.


Solution

  • Html.Raw() uses the HtmlHelper class but Url.Action() the UrlHelper Class so you would need to pass that as well

    @using System.Web.Mvc
    @helper Tabel(HtmlHelper html, UrlHelper url)
    { 
        html.Raw(url.Action("Index", "Home"))
    }