asp.net – Razor base type / Templated Razor使用“using”关键字代理
发布时间:2020-10-19 11:39:50 所属栏目: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中的GridView中定义CellPadding
- asp.net – 在IHTTPHandler中以编程方式创建System.Web.UI.
- asp.net-mvc-4 – WepApi控制器是否应该返回viewmodels
- asp.net 删除项目文件/文件夹IIS重启,Session丢失问题
- asp.net-mvc – 在MVC命令,优先级和功能问题中授权属性
- 单元测试 – 如何在ASP MVC 5(Microsoft.AspNet.Identity)中
- asp.net-mvc – 在ASP.NET MVC中获取当前操作/控制器的自定
- asp.net实现文件下载的代码
- 什么是使用aspnet_compiler.exe预编译ASP.NET项目的优势?
- asp.net 2.0中利用Ajax2.0实现JSON传送大量页面数据
推荐文章
站长推荐
- asp.net – 如何打破VB.NET中的“if”块
- asp.net – 如何以编程方式从LDAP检索信息
- asp.net-mvc-3 – DropDownListFor Unobtrusive
- asp.net-mvc – 图像URL中的符号MVC导致一个潜在
- asp.net-mvc – 使用mvc的主要目的
- asp.net-mvc – 从Api控制器内生成绝对的url to
- asp.net – VS插件:查看标记.存在这样的事情吗?
- ASP.Net – AJAX UpdatePanel中的Javascript
- 从代码隐藏调用ASP.NET Web API
- asp.net-mvc – ASP.NET MVC视图模型的最佳做法
热点阅读
