RPM1984
RPM1984

Reputation: 73112

Access WebViewPage from custom HTML Helper

I've got a ASP.NET MVC 3 Razor Web Application.

I've got a WebViewPage extension:

public static bool Blah(this WebViewPage webViewPage)
{
   return blah && blah;
}

And i want to access this from my HtmlHelper extension:

public static MvcHtmlString BlahHelper(this HtmlHelper htmlHelper, string linkText, string actionName)
{
   // how can i get access to the WebViewPage extension method here?
}

I can of course duplicate the functionality of the WebViewPage extension if i had to, but just wondering if it's possible to access it from the HTML helper.

Upvotes: 4

Views: 2774

Answers (4)

Andreas
Andreas

Reputation: 2242

I had the same problem and the accepted answer led me to the solution (+1). Maybe this hint helps somebody else too. Additionally I had to use the generic version of WebViewPage inside a strongly type view. Otherwise I got a type cast exception.

public static MvcHtmlString ToBodyEnd<TModel>(this HtmlHelper<TModel> htmlHelper, ...) {
       var vp = (DerivedWebViewPage<TModel>)htmlHelper.ViewDataContainer;
    //... more code ...
    }

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1038730

// Warning: this cast will crash
// if you are not using the Razor view engine
var wvp = (WebViewPage)htmlHelper.ViewDataContainer;
var result = wvp.Blah();

Upvotes: 10

ryudice
ryudice

Reputation: 37366

I would try:

((WebViewPage)htmlHelper.ViewContext.View). Blah()

Upvotes: 0

SLaks
SLaks

Reputation: 887365

You should change that method to extend HttpContextBase, which you can access from both HtmlHelper and WebViewPage.

Upvotes: 0

Related Questions