Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing MVC HtmlHelper extensions from Razor declarative views
    primarykey
    data
    text
    <p>I was trying to create a Razor declarative helper in my App_Code folder for an MVC 3 RTM project.</p> <p>The problem I ran into was that the MVC HtmlHelper extensions, like ActionLink, aren't available. This is because the compiled helpers derive from <code>System.Web.WebPages.HelperPage</code>, and though it exposes an <code>Html</code> property, its of type <code>System.Web.WebPages.HtmlHelper</code> rather than <code>System.Web.Mvc.HtmlHelper</code>.</p> <p>An example of the kind of error I was getting is:</p> <blockquote> <p>'System.Web.Mvc.HtmlHelper' does not contain a definition for 'ActionLink' and no extension method 'ActionLink' accepting a first argument of type 'System.Web.Mvc.HtmlHelper' could be found (are you missing a using directive or an assembly reference?)</p> </blockquote> <p>My only solution has been to create my own HelperPage and override the Html property:</p> <pre><code>using System.Web.WebPages; public class HelperPage : System.Web.WebPages.HelperPage { // Workaround - exposes the MVC HtmlHelper instead of the normal helper public static new HtmlHelper Html { get { return ((System.Web.Mvc.WebViewPage) WebPageContext.Current.Page).Html; } } } </code></pre> <p>I then have to write the following at the top of every helper:</p> <pre><code>@inherits FunnelWeb.Web.App_Code.HelperPage @using System.Web.Mvc @using System.Web.Mvc.Html @helper DoSomething() { @Html.ActionLink("Index", "Home") } </code></pre> <p>Is it meant to be this hard in MVC 3, or am I doing something wrong?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload