asp.net – Razor base type / Templated Razor使用“using”关键字代理
发布时间:2020-10-19 09:38:01 所属栏目: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 o
|
我目前有一个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 – 在ASP.NET MVC中的LinkButton
- .net – ReportViewer 2010无法评估表达式
- 如何设置特定于ASP.NET请求的log4net上下文属性?
- asp.net-mvc – 程序集使用System.Web.Http 5.1,它比引用的
- asp.net – 如何在MVC 3中设置图表系列颜色?
- asp.net-mvc – 如何与NopCommerce MVC合作
- asp.net-mvc – MVC会话过期而不是身份验证
- 详解ASP.NET Core 中的框架级依赖注入
- Asp.Net 5分钟实现网页实时监控
- asp.net-mvc – ASP.NET MVC:添加将DisplayName合并到自定
推荐文章
站长推荐
- Asp.net 实现Session分布式储存(Redis,Mongodb,M
- asp.net – 在VS Code中指定localhost端口的位置
- asp.net实现生成缩略图及给原始图加水印的方法示
- asp.net – 在Application_BeginRequest中设置会
- asp.net下大文件上传知识整理
- asp.net-mvc – ASP .Net MVC 3:子动作和重定向
- ASP.NET MVC下Ajax.BeginForm方式无刷新提交表单
- asp.net后台cs中的JSON格式变量在前台Js中调用方
- 我如何让Fiddler捕获我的MVC应用程序向我的ASP.N
- asp.net – 如何使用JwtSecurityTokenHandler和J
热点阅读
