【问题标题】:Using ASP.NET MVC Html Helpers in a standard web forms project在标准 Web 表单项目中使用 ASP.NET MVC Html Helpers
【发布时间】:2010-01-15 17:05:01
【问题描述】:

因此,例如在 web 表单 aspx 页面的代码中,我希望能够执行类似的操作

字符串 textBoxHtml = Html.TextBox("MyTextBox");

这可能吗?

源代码是否可用于 fork 网络表单?

【问题讨论】:

  • 是的,我想用@Html.Partial() 来做这件事,以实现重用标头HTML 标记的目标。

标签: asp.net-mvc webforms html-helper


【解决方案1】:
  1. 可能吗?是的。

  2. 完整的 MVC 源代码位于: http://www.microsoft.com/downloads/details.aspx?FamilyID=53289097-73ce-43bf-b6a6-35e00103cb4b&displaylang=en

祝你好运!

您很快就会发现,从 MVC 中提取一些代码就像只想要一根香蕉并让大猩猩拿着它。 ;)

【讨论】:

  • 我们需要偷猎者的时候他们在哪里?
【解决方案2】:

到目前为止,这是对我有用的东西。

public static class PageCommon
{
    public static System.Web.Mvc.UrlHelper GetUrlHelper(this System.Web.UI.Control c)
    {
        var helper = new System.Web.Mvc.UrlHelper(c.Page.Request.RequestContext);
        return helper;
    }
    class ViewDataBag : IViewDataContainer
    {
        ViewDataDictionary vdd = new ViewDataDictionary();
        public ViewDataDictionary ViewData
        {
            get
            {
                return vdd;
            }
            set
            {
                vdd = value;
            }
        }
    }
    public static System.Web.Mvc.HtmlHelper GetHtmlHelper(this System.Web.UI.Control c)
    {
        IViewDataContainer x;

        var v = new System.Web.Mvc.ViewContext();
        var helper = new System.Web.Mvc.HtmlHelper(v, new ViewDataBag());
        return helper;
    }

【讨论】:

  • 使用这个帮助器来呈现这样的输入:,给出一个 NRE:[NullReferenceException: Object reference not set to an instance对象的。] System.Web.Mvc.Html.InputExtensions.InputHelper(HtmlHelper htmlHelper,InputType inputType,ModelMetadata 元数据,字符串名称,对象值,布尔 useViewData,布尔 isChecked,布尔 setId,布尔 isExplicitValue,IDictionary`2 htmlAttributes)+ 43 System.Web.Mvc.Html.InputExtensions.TextBox(HtmlHelper htmlHelper, String name) +43 有什么想法吗?
  • 在调试中,究竟什么是null?
  • 取决于您使用的 ext 方法:对于 .TextBox(..) 将是 HttpContext (这是它试图从中获取值的地方),对于 .BeginForm 它将是 .Writer 属性(所以它可以呈现打开/关闭 HTML 标签)。最后的课程可能看起来像:gist.github.com/esteewhy/6450385
猜你喜欢
  • 2010-12-18
  • 1970-01-01
  • 2017-07-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-09
  • 2016-11-05
  • 1970-01-01
相关资源
最近更新 更多