你如何创建ASP.NET MVC 2.0视图外的的HtmlHelper? [英] How do you create an HtmlHelper outside of a view in ASP.NET MVC 2.0?

查看:222
本文介绍了你如何创建ASP.NET MVC 2.0视图外的的HtmlHelper?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在升级ASP.NET MVC 1.0应用程序到2.0版本和一些code的过程中需要使用它需要的HtmlHelper渲染LinkExtensions的。虽然我们知道,一些code的不正确地遵循MVC模型,并在需要重新编码的过程中,我们需要一些东西来工作,所以去构建应用程序。

下面是在ASP.NET MVC 1.0的工作当前的语法,我们有:

 公共静态的HtmlHelper GetHtmlHelper(ControllerContext上下文)
{
    返回新的HtmlHelper(新ViewContext(背景下,
                                          新WebFormView(HtmlHelperView),
                                          新的ViewDataDictionary()
                                          新TempDataDictionary()),
                          新的ViewPage());
}

这是我们得到的错误如下:


  

错误1'System.Web.Mvc.ViewContext'不包含一个构造函数4个参数



解决方案

有是一个额外的参数,这需要的TextWriter

  VAR viewContext =新ViewContext(
    的背景下,
    新WebFormView(HtmlHelperView),
    新的ViewDataDictionary()
    新TempDataDictionary(),
    context.HttpContext.Response.Output
);

这里的问题是,为什么你需要实例化一个的HtmlHelper 自己,而不是使用在视图中提供的?

We are in the process of upgrading an ASP.NET MVC 1.0 application to the 2.0 release and some of the code requires the use of LinkExtensions which require an HtmlHelper to render. While we know that some of the code doesn't follow the MVC model correctly and are in the process of recoding as needed, we need something to work so get the application to build.

Here is the current syntax that we have that works under 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 that we are getting is as follows:

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

解决方案

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?

这篇关于你如何创建ASP.NET MVC 2.0视图外的的HtmlHelper?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆