Access WebViewPage from a custom HTML helper

I have an ASP.NET MVC 3 Razor web application.

I have 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 could, of course, duplicate the functionality of the WebViewPage extension if I had to, but just wondering if it is possible to access it from the HTML helper.

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

You must change this method to extend the HttpContextBase , which can be accessed from both the HtmlHelper and WebViewPage .

0
source

I would try:

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

I had the same problem and the accepted answer led me to a solution (+1). Perhaps this hint also helps someone else. In addition, I had to use the universal version of WebViewPage inside a strict view. Otherwise, I got a type exception.

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

Source: https://habr.com/ru/post/1387487/


All Articles