如何在自定义 HtmlHelper 中使用 C# 对象?
我尝试使用复制大多数标准 HtmlHelpers 的语法,其中使用 object
将 Html 属性添加到生成的标记。
我想要类似的内容:
<%: Html.MyCustomHelper("SomeValue", new { id="myID", @class="myClass" })%>
如何访问该新对象的键,以便可以从视图中添加属性?
这会使用反射吗?我听说过这个词,但我不太熟悉。
I am trying to use replicate the syntax for most of the standard HtmlHelpers where an object
is used to add Html attributes to a generated tag.
I would like to have something like:
<%: Html.MyCustomHelper("SomeValue", new { id="myID", @class="myClass" })%>
How do I access the keys of that new object so I can add attributes from the view?
Would this use reflection? I've heard that word thrown around a bit, but I'm not familiar with it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
内置帮助器方法使用
RouteValueDictionary
将object
转换为字典。它们还提供了两种用于接受 HTML 属性的重载,一种接受object
,另一种接受IDictionary
:The built-in helper methods use
RouteValueDictionary
to convert theobject
into a dictionary. They also provide two overloads for accepting HTML attributes, one which accepts anobject
, and one which accepts anIDictionary<string, object>
:此功能是由 MVC 团队的 Eilon Lipton 创建的。详细信息在这里 - 并下载提供的代码来推出您自己的代码。
http ://weblogs.asp.net/leftslipper/archive/2007/09/24/using-c-3-0-anonymous-types-as-dictionaries.aspx
This ability was created by Eilon Lipton on the MVC Team. Details are here - and download the provided code to roll your own.
http://weblogs.asp.net/leftslipper/archive/2007/09/24/using-c-3-0-anonymous-types-as-dictionaries.aspx