asp.net-mvc – 什么是HtmlHelper渲染htmlAttributes对象的方法
我正在构建一个编辑器模板,它将接受htmlAttributes对象.
示例很简单. 在主视图中我有类似的东西 @Html.EditorFor(Function(x) x.SomeProperty,New With {.htmlAttributes = New With {.class = "form-control"}}). 在编辑模板中,我需要编写class =“form-control”属性. 如果在模板中我使用类似@Html.TextBoxFor(Function(x)x,ViewData(“htmlAttributes”))的东西,那么一切都还可以.但是如果我手动构建一个标签,我就无法输出htmlAttributes,即我构建< input type =“text”{htmlAttributes应该在这里} /> 是否有任何公共方法可以正确地在标记内呈现HTML属性? 解决方法
我不确定你到底在找什么.传统上,编辑器模板只会传递htmlAttributes.例如:
视图 @Html.EditorFor(m => m.FooString,new { htmlAttributes = new { @class = "foo" } }) String.cshtml @Html.TextBox("",ViewData.TemplateInfo.FormattedModelValue.ToString(),ViewData["htmlAttributes"]) 如果您正在询问如何执行类似设置默认值的操作,然后可以通过传递htmlAttributes来覆盖或添加默认值.那么,你在那里几乎就是你自己. MVC中没有任何东西可以帮助你(至少完全没有).但是,我确实编写了自己的HtmlHelper扩展来处理这个问题.我实际上写了一个blog post来解释它的作用以及如何使用它.我建议你检查一下,但我会在这里发布代码以保证完整性. using System.Collections.Generic; using System.Linq; using System.Web.Mvc; using System.Web.Routing; public static partial class HtmlHelperExtensions { public static IDictionary<string,object> MergeHtmlAttributes(this HtmlHelper helper,object htmlAttributesObject,object defaultHtmlAttributesObject) { var concatKeys = new string[] { "class" }; var htmlAttributesDict = htmlAttributesObject as IDictionary<string,object>; var defaultHtmlAttributesDict = defaultHtmlAttributesObject as IDictionary<string,object>; RouteValueDictionary htmlAttributes = (htmlAttributesDict != null) ? new RouteValueDictionary(htmlAttributesDict) : HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributesObject); RouteValueDictionary defaultHtmlAttributes = (defaultHtmlAttributesDict != null) ? new RouteValueDictionary(defaultHtmlAttributesDict) : HtmlHelper.AnonymousObjectToHtmlAttributes(defaultHtmlAttributesObject); foreach (var item in htmlAttributes) { if (concatKeys.Contains(item.Key)) { defaultHtmlAttributes[item.Key] = (defaultHtmlAttributes[item.Key] != null) ? string.Format("{0} {1}",defaultHtmlAttributes[item.Key],item.Value) : item.Value; } else { defaultHtmlAttributes[item.Key] = item.Value; } } return defaultHtmlAttributes; } } 然后,在您的编辑器模板中(此示例中为Date.cshtml): @{ var defaultHtmlAttributesObject = new { type = "date",@class = "form-control" }; var htmlAttributesObject = ViewData["htmlAttributes"] ?? new { }; var htmlAttributes = Html.MergeHtmlAttributes(htmlAttributesObject,defaultHtmlAttributesObject); } @Html.TextBox("",htmlAttributes) UPDATE
为什么?帮助器版本没有任何你无法做到的事情.但是,如果你坚持走这条路,那么你将很难用Razor做这件事.假设您实际上可以在没有Razor语法错误的情况下编写它,那么代码将变得虚拟不可读.我建议在纯C#中使用TagBuilder和/或StringBuilder来构造一个字符串,然后确保返回/设置一个MvcHtmlString类型的变量: var output = new MvcHtmlString(builder.ToString()); 但是,如果你走得那么远,它会否定使用编辑器模板的目的,除非你试图覆盖其中一个默认的编辑器模板.无论如何,我建议您只创建自己的HtmlHelper扩展,然后直接在视图中使用它或在编辑器模板中使用它. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- ASP.NET Core集成现有系统认证
- asp.net – 如何在Visual Studio 2010中设置Umbraco cms?
- asp.net-core – Stream的意外结束,内容可能已被另一个组件
- asp.net-mvc – 发生异常后不要刷新会话 – NHibernate
- asp.net-mvc – 用于表行的Asp.Net MVC WebGrid帮助器自定义
- asp.net-mvc – 在asp.net MVC3中调用局部视图
- 如何在ASP.NET中重命名文件?
- ASP.NET:存储应用程序设置的位置?
- asp.net-mvc – 为什么我的Html帮助者没有智能感?
- asp.net-core – Controller.json设置Serialization.Refere
- asp.net – 创建没有ASMX文件的Web服务?
- 如何使用Feature Toggle Nuget Package在.Net Co
- asp.net-mvc-3 – Value Injecter是否会映射集合
- asp.net-mvc – 如何在ASP.NET MVC模型中获取当前
- 如何确定ASP.NET站点是否存在内存泄漏
- 解析ASP.NET MVC使用Java中的Jackson JSON库返回
- asp.net – HTML5编辑器从微软词语粘贴时丢失格式
- asp.net-mvc – ASP.NET MVC中的本地化
- 获取ASP.NET会话上次访问时间(或超时)
- asp.net-mvc – 获取MVC忽略到站点根目录的路由