当前位置: 首页 > 编程语言 > C#

AspMVC4CreateacustomhtmlhelpermethodsimilartoHtml.BeginForm分享

时间:2023-04-11 01:47:55 C#

AspMVC4CreateacustomhtmlhelpermethodsimilartoHtml.BeginForm我有以下html:可用语言:我想制作一个自定义html助手并像这样使用它(类似于Html.BeginForm)@Html.BeginView(){可用语言:}我开始制作我的助手方法varparentDiv=newTagBuilder("div");parentDiv.MergeAttribute("数据绑定","preventBinding:true");返回新的MvcHtmlString();我阅读了如何制作基本的html助手,但是我看到的示例没有提供有关如何在我的情况下制作它的信息。我是aspmvc的新手,非常感谢您的帮助。更新2:显然我遗漏了一些东西。在我看来,我称之为:@Html.BeginView(){test}一切看起来都很好,甚至是智能感知。但是在浏览器中的输出如下:Omega.UI.WebMvc.Helpers.BeginViewHelper+MyView{test}这是我的辅助方法:namespaceOmega.UI.WebMvc.Helpers{publicstaticclassBeginViewHelper{publicstaticIDisposableBeginView(thisHtmlHelper助手){helper.ViewContext.Writer.Write("");helper.ViewContext.Writer.Write("");返回新的MyView(助手);}类MyView:IDisposable{私人HtmlHelper_helper;publicMyView(HtmlHelperhelper){this._helper=helper;}publicvoidDispose(){this._helper.ViewContext.Writer.Write("");this._helper.ViewContext.Writer.Write("");}}}}我在~/命名空间注册在Views/web.config不能返回MvcHtmlString。相反,您应该将html写入编写器并返回实现IDisposable的类,并且在调用Dispose期间将写入HTML的结尾部分。publicstaticclassBeginViewHelper{publicstaticIDisposableBeginView(thisHtmlHelperhelper,stringviewId){helper.ViewContext.Writer.Write(string.Format("",viewId));返回新的MyView(助手);}classMyView:IDisposable{privateHtmlHelper助手;publicMyView(HtmlHelperhelper){this.helper=helper;}publicvoidDispose(){this.helper.ViewContext.Writer.Write("");如果你有一个更复杂的结构,你可以尝试使用TagBuilder:TagBuildertb=newTagBuilder("div");helper.ViewContext.Writer.Write(tb.ToString(TagRenderMode.StartTag));Slawek有正确的答案,但我想我会用我的经验来补充它。我想创建一个助手来在页面上显示一个小部件(几乎就像一个带有标题栏和内容部分的jQuery小部件)。有用的东西:@using(Html.BeginWidget("WidgetTitle",3/*columnWidth*/)){@*WidgetContents*@}MVC源代码使用类似于Slawek发布的东西,但我想把开头帮助程序和实际对象中的结束标记不“整洁”,也没有将焦点放在核心位置。如果我想改变外观,我现在在两个地方做,而不是我认为合乎逻辑的地方。所以我想出了以下内容://////Widgetcontainer/////////我们将它设为IDIsposable以便我们可以像Html.BeginForm一样使用它并且当@using(){}块结束时,///小部件内容的结尾被输出。///publicclassHtmlWidget:IDisposable{#regionCTor//存储一些引用以便于使用privatereadonlyViewContextviewContext;私有只读System.IO.TextWritertextWriter;//////通过将视图上下文传递给它来初始化框(这样我们就可以///引用流编写器)然后调用BeginWidget方法///开始小部件的输出//////引用viewcontext///小部件的标题///小部件的宽度(列布局)publicHtmlWidget(ViewContextviewContext,Stringtitle,Int32columnWidth=6){if(viewContext==null)thrownewArgumentNullException("viewContext");如果(String.IsNullOrWhiteSpace(title))抛出新的ArgumentNullException("title");如果(columnWidth12)抛出新的ArgumentOutOfRangeException("columnWidth","Valuemustbefrom1-12");this.viewContext=viewContext;this.textWriter=this.viewContext.Writer;this.BeginWidget(title,columnWidth);}#endregion#region小部件渲染//////输出小部件的开始HTML//////小部件的标题///小部件宽度(列布局)protectedvirtualvoidBeginWidget(Stringtitle,Int32columnWidth){标题=HttpUtility.HtmlDecode(标题);varhtml=newSystem.Text.StringBuilder();html.AppendFormat("",columnWidth).AppendLine();html.AppendFormat("{0}",title).AppendLine();html.Append("").AppendLine();this.textWriter.WriteLine(html.ToString());}//////输出小部件的结束HTML///protectedvirtualvoidEndWidget(){this.textWriter.WriteLine("");}#endregion#regionIDisposableprivateBooleanisDisposed;publicvoidDispose(){this.Dispose(true);GC.SuppressFinalize(这个);}publicvirtualvoidDispose(Booleandisposing){if(!this.isDisposed){this.isDisposed=true;这个.EndWidget();this.textWriter.Flush();然后,这使我们的帮助程序更清晰(并且在两个地方没有UI代码):,列宽);然后我们可以像本文开头那样使用asp.netmvc的BeginForm方法来返回MvcForm类的IDisposable实例。如果您查看codeplex上的asp.netmvc代码,您可以了解asp.netmvc团队是如何开发此功能的。查看这些链接: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以上是C#学习教程:AspMVC4创建自定义html类似Html.BeginForm如果辅助方法分享的所有内容对你都有用,需要了解更多的C#学习教程,希望大家多多关注。本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处: