asp.net – Razor base type / Templated Razor使用“using”关
发布时间:2020-12-15 20:40:41 所属栏目:asp.Net 来源:网络整理
导读:我目前有一个div容器,用于表单中的所有输入字段,类似于: div class="ux-single-field ui-widget-content ui-corner-all" @Html.LabelFor(m = m.Name) @Html.TextBoxFor(m = m.Name)/div 我想知道如何使用templated razor delegate(或any other trick)封装它
我目前有一个div容器,用于表单中的所有输入字段,类似于:
<div class="ux-single-field ui-widget-content ui-corner-all"> @Html.LabelFor(m => m.Name) @Html.TextBoxFor(m => m.Name) </div> 我想知道如何使用templated razor delegate(或any other trick)封装它,所以就像我们使用: @using (Html.BeginForm()) { } 我可以简单地包装我的元素: @using (Html.ContentField()) { @Html.LabelFor(m => m.Name) @Html.TextBoxFor(m => m.Name) } 解决方法
使用Razor View引擎,这是有效的:
namespace MyProject.Web.Helpers.Extensions { public static class LayoutExtensions { public static ContentField BeginContentField(this HtmlHelper htmlHelper) { return FormHelper(htmlHelper,new RouteValueDictionary()); } public static ContentField BeginContentField(this HtmlHelper htmlHelper,RouteValueDictionary htmlAttributes) { return FormHelper(htmlHelper,htmlAttributes); } public static void EndContentField(this HtmlHelper htmlHelper) { htmlHelper.ViewContext.Writer.Write("</div>"); } private static ContentField FormHelper(this HtmlHelper htmlHelper,IDictionary<string,object> htmlAttributes) { TagBuilder tagBuilder = new TagBuilder("div"); tagBuilder.MergeAttributes(htmlAttributes); tagBuilder.MergeAttribute("class","ux-single-field ui-widget-content ui-corner-all"); htmlHelper.ViewContext.Writer.Write(tagBuilder.ToString(TagRenderMode.StartTag)); return new ContentField(htmlHelper.ViewContext.Writer); } } public class ContentField : IDisposable { private bool _disposed; private readonly TextWriter _writer; public ContentField(TextWriter writer) { if (writer == null) throw new ArgumentNullException("writer"); _writer = writer; } [SuppressMessage("Microsoft.Security","CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")] public void Dispose() { Dispose(true /* disposing */); GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { if (!_disposed) { _disposed = true; _writer.Write("</div>"); } } public void EndForm() { Dispose(true); } } } 仅供参考:使用旧的ASPX引擎,here’s how to do it. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – 在IIS7上设置mvc应用程序?
- asp.net – 上下文菜单没有“添加脚手架”或“添加控制器”
- asp.net-web-api – 批量发布实体到WebAPI?
- ASP.NET WebForm的“页面生命周期”是什么?
- 验证 – ASP.NET MVC将null转换为零长度字符串
- asp.net – C#:GDI:使用位图的保存方法过度写入图像
- SQL Server 2005:一个使用新创建的User的问题和解决方法
- asp.net-mvc – 来自父Razor布局的@Functions继承
- ASP.NET开发常用简单实用的方法
- asp.net-mvc-3 – .NET MVC 3提交按钮做了不同的事情
推荐文章
站长推荐
- asp.net-mvc – angular js和asp.net mvc 4示例应
- asp.net – 升级到.net 4.0后无法加载文件或程序
- asp.net – JSON.NET读取JObject时出错
- asp.net – 用于检索用户名的IPrincipal或Member
- asp.net-mvc-3 – MVC3 – 类型double与前导零的
- asp.net – 在mod_mono和Apache上的Appdomain回收
- ASP.NET Core 在 Swagger UI 中显示自定义的 Hea
- asp.net – Asp Button悬停和CSS
- asp.net-mvc – 从Visual Web Developer Express
- asp.net-core – 在Microsoft.AspNet.Http.HttpC
热点阅读