How to create an HtmlHelper outside of a view in ASP.NET MVC 2.0?

We are in the process of updating the ASP.NET MVC 1.0 application to release 2.0, and some codes require the use of LinkExtensions, which requires the visualization of HtmlHelper. Although we know that some of the code does not correspond to the MVC model correctly and, if necessary, is transcoded, we need to work something for the application to be created.

Here is the current syntax that we have that works in ASP.NET MVC 1.0:

public static HtmlHelper GetHtmlHelper(ControllerContext context) { return new HtmlHelper(new ViewContext(context, new WebFormView("HtmlHelperView"), new ViewDataDictionary(), new TempDataDictionary()), new ViewPage()); } 

The error we get is the following:

Error 1 'System.Web.Mvc.ViewContext' does not contain a constructor that takes 4 arguments

+4
source share
2 answers

There is an additional argument that TextWriter accepts :

 var viewContext = new ViewContext( context, new WebFormView("HtmlHelperView"), new ViewDataDictionary(), new TempDataDictionary(), context.HttpContext.Response.Output ); 

This raises the question: why do you need to create an instance of htmlHelper yourself, and not use the one that is presented in the views?

+5
source

The problem (as the error message indicates) is that the ViewContext constructor is no longer a constructor that accepts 4 parameters. They added a fifth, which is a text editor. You can create a viewcontext this way:

 new ViewContext(context, new WebFormView("HtmlHelperView"), new ViewDataDictionary(), new TempDataDictionary()), new ViewPage(), context.HttpContext.Response.Output); 
+2
source

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


All Articles