Andrus
Andrus

Reputation: 27953

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.

Upvotes: 4

Views: 3055

Answers (2)

Simon Hostettler
Simon Hostettler

Reputation: 11

This worked for me too without passing the arguments:

@helper Tabel()
{ 
    var html = ((System.Web.Mvc.WebViewPage)WebPageContext.Current.Page).Html;
    var url = ((System.Web.Mvc.WebViewPage)WebPageContext.Current.Page).Url;

    html.Raw(url.Action("Index", "Home"))
}

Upvotes: 1

user3559349
user3559349

Reputation:

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"))
}

Upvotes: 5

Related Questions