Asp MVC 4创建类似于Html.BeginForm的自定义html辅助方法

我有以下html:

我想制作一个自定义的html助手并像这样使用它(类似于Html.BeginForm

 @Html.BeginView() { 
}

我开始制作我的助手方法

 public static class BeginViewHelper { public static MvcHtmlString BeginView(this HtmlHelper helper, string viewId) { var parentDiv = new TagBuilder("div"); parentDiv.MergeAttribute("data-bind", "preventBinding: true"); return new MvcHtmlString(); } } 

我阅读了如何制作基本的html助手,但我看到的例子并未向我提供如何在我的情况下制作它的信息。 我是asp mvc的新手,非常感谢每一位帮助。

更新2:

显然我错过了一些东西。 我在我的观点中称之为:

 @Html.BeginView() { 
}

一切似乎很好,甚至有智能感知。 但浏览器中的输出如下:

 Omega.UI.WebMvc.Helpers.BeginViewHelper+MyView { test } 

这是我的帮手方法:

 namespace Omega.UI.WebMvc.Helpers { public static class BeginViewHelper { public static IDisposable BeginView(this HtmlHelper helper) { helper.ViewContext.Writer.Write("
"); helper.ViewContext.Writer.Write("
"); return new MyView(helper); } class MyView : IDisposable { private HtmlHelper _helper; public MyView(HtmlHelper helper) { this._helper = helper; } public void Dispose() { this._helper.ViewContext.Writer.Write("
"); this._helper.ViewContext.Writer.Write("
"); } } } }

我在〜/ Views / web.config中注册了名称空间

   

你不能返回MvcHtmlString。 而不是你应该将html写入编写器并返回实现IDisposable的类,并且在调用Dispose期间将写入HTML的结束部分。

 public static class BeginViewHelper { public static IDisposable BeginView(this HtmlHelper helper, string viewId) { helper.ViewContext.Writer.Write(string.Format("
", viewId)); return new MyView(helper); } class MyView : IDisposable { private HtmlHelper helper; public MyView(HtmlHelper helper) { this.helper = helper; } public void Dispose() { this.helper.ViewContext.Writer.Write("
"); } } }

如果您有更复杂的结构,可以尝试使用TagBuilder:

 TagBuilder tb = new TagBuilder("div"); helper.ViewContext.Writer.Write(tb.ToString(TagRenderMode.StartTag)); 

Slawek有正确的答案 ,但我想我会用我的经验补充它。

我想创建一个帮助器来在页面上显示小部件(几乎就像带有标题栏和内容部分的jQuery小部件)。 有效的东西:

 @using (Html.BeginWidget("Widget Title", 3 /* columnWidth */)) { @* Widget Contents *@ } 

MVC源使用类似于Slawek发布的内容,但我觉得将开始标记放在帮助器中并且实际对象中的结束标记不是“整洁”,也没有在核心位置保留关注。 如果我想改变外观,我现在在两个地方这样做,而不是我认为是一个合乎逻辑的地方。 所以我想出了以下内容:

 ///  /// Widget container ///  ///  /// We make it IDIsposable so we can use it like Html.BeginForm and when the @using(){} block has ended, /// the end of the widget's content is output. ///  public class HtmlWidget : IDisposable { #region CTor // store some references for ease of use private readonly ViewContext viewContext; private readonly System.IO.TextWriter textWriter; ///  /// Initialize the box by passing it the view context (so we can /// reference the stream writer) Then call the BeginWidget method /// to begin the output of the widget ///  /// Reference to the viewcontext /// Title of the widget /// Width of the widget (column layout) public HtmlWidget(ViewContext viewContext, String title, Int32 columnWidth = 6) { if (viewContext == null) throw new ArgumentNullException("viewContext"); if (String.IsNullOrWhiteSpace(title)) throw new ArgumentNullException("title"); if (columnWidth < 1 || columnWidth > 12) throw new ArgumentOutOfRangeException("columnWidth", "Value must be from 1-12"); this.viewContext = viewContext; this.textWriter = this.viewContext.Writer; this.BeginWidget(title, columnWidth); } #endregion #region Widget rendering ///  /// Outputs the opening HTML for the widget ///  /// Title of the widget /// Widget width (columns layout) protected virtual void BeginWidget(String title, Int32 columnWidth) { title = HttpUtility.HtmlDecode(title); var html = new System.Text.StringBuilder(); html.AppendFormat("
", columnWidth).AppendLine(); html.AppendFormat("
{0}
", title).AppendLine(); html.Append("
").AppendLine(); this.textWriter.WriteLine(html.ToString()); } /// /// Outputs the closing HTML for the widget /// protected virtual void EndWidget() { this.textWriter.WriteLine("
"); } #endregion #region IDisposable private Boolean isDisposed; public void Dispose() { this.Dispose(true); GC.SuppressFinalize(this); } public virtual void Dispose(Boolean disposing) { if (!this.isDisposed) { this.isDisposed = true; this.EndWidget(); this.textWriter.Flush(); } } #endregion }

然后,这使得我们的助手更加清晰(并且在两个地方没有UI代码):

 public static HtmlWidget BeginWidget(this HtmlHelper htmlHelper, String title, Int32 columnWidth = 12) { return new HtmlWidget(htmlHelper.ViewContext, title, columnWidth); } 

然后我们可以像在这篇文章的顶部那样使用它。

asp.net mvc的BeginForm方法返回MvcForm类的IDisposable实例。 如果您查看codeplex上的asp.net mvc代码 ,可以查看asp.net mvc团队如何开发此function。

看看这些链接:

MvcForm类(IDisposable) http://aspnetwebstack.codeplex.com/SourceControl/changeset/view/8b17c2c49f88#src/System.Web.Mvc/Html/MvcForm.cs

表单扩展(用于html帮助程序) http://aspnetwebstack.codeplex.com/SourceControl/changeset/view/8b17c2c49f88#src/System.Web.Mvc/Html/FormExtensions.cs