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

你离开我真会死。 提交于 2019-12-05 14:54:18

There's an additional argument which takes a TextWriter:

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

The question here is why would you need to instantiate a htmlHelper yourself instead of using the one that's provided in the views?

The problem is (as the error message suggests) that there no longer is a constructor of ViewContext that takes 4 parameters. They have added a fifth that is a textwriter. You can create a viewcontext in this way:

new ViewContext(context,
                                      new WebFormView("HtmlHelperView"),
                                      new ViewDataDictionary(),
                                      new TempDataDictionary()),
                      new ViewPage(), context.HttpContext.Response.Output);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!