匿名类和IDictionary的区别ASP.NET MVC 中的 htmlAttributes?
例如,如果您检查这两个扩展方法,唯一的区别是 htmlAttributes 的类型,因此您可以通过两种不同的方式传递 htmlAttributes:
public static MvcHtmlString TextBoxFor<TModel, TProperty>(
this HtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, TProperty>> expression,
IDictionary<string, object> htmlAttributes);
public static MvcHtmlString TextBoxFor<TModel, TProperty>(
this HtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, TProperty>> expression,
object htmlAttributes);
并以以下任一方式使用它们:
@Html.TextBoxFor(model => model.TagLine,
new { @placeholder = "We live to make art." })
@Html.TextBoxFor(model => model.TagLine,
new Dictionary<string, object> {
{ "placeholder", "We live to make art." } })
我已经检查了 MVC 源代码,并且我知道它们在后台使用相同的方法,但是接受匿名对象的方法使用 HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes) 使匿名对象成为字典。
在我看来,使用匿名对象的视图更干净。你们觉得怎么样?使用匿名对象有什么缺点吗?
For example if you check these two extension methods the only difference is type of htmlAttributes so you can pass your htmlAttributes in two different ways:
public static MvcHtmlString TextBoxFor<TModel, TProperty>(
this HtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, TProperty>> expression,
IDictionary<string, object> htmlAttributes);
public static MvcHtmlString TextBoxFor<TModel, TProperty>(
this HtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, TProperty>> expression,
object htmlAttributes);
And use them in either of these ways:
@Html.TextBoxFor(model => model.TagLine,
new { @placeholder = "We live to make art." })
@Html.TextBoxFor(model => model.TagLine,
new Dictionary<string, object> {
{ "placeholder", "We live to make art." } })
I have checked MVC source code and I know in the background they use same method, but the one which accepts the anonymous object uses HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)
to make the anonymous object a dictionary.
In my point of view, views are cleaner to use anonymous object. What do you think guys? Are there any drawbacks to using an anonymous object?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
没有太大区别,但是使用匿名对象对于调用者来说是一种更清晰、更易读的语法,现在被认为是更标准的做法。如果您热衷于微优化,那么使用 IDictionary 可能会带来轻微的、可以忽略不计的性能优势。
IDictionary 重载选项可能自 ASP.NET MVC 1.0 CTP 时代起就一直存在,当时 C# 3.0 和匿名对象还很新。 Eilon Lipton 的博客文章建议 使用C# 3.0 匿名类型作为字典 提供了一些背景知识。
There's not too much difference, however using the anonymous object is a cleaner and more readable syntax for the caller, and now considered more standard practice. There might be a slight, negligible performance benefit to using IDictionary if you're a fan of micro-optimisation.
The IDictionary overload option has probably stuck since ASP.NET MVC 1.0 CTP days when C# 3.0 and anonymous objects were still quite new. Eilon Lipton's blog post proposing Using C# 3.0 Anonymous Types as Dictionaries gives some background.
IDictionary
的存在有特殊原因。如果您想在扩展函数中调整 htmlAttributes 参数,那么这是可能的。我有几个 Html.TextBox 函数的扩展。例如,我有一个名为:TextBoxOrDisplayForNoDiaCritics
的函数。此函数启用文本框或显示禁用的文本框。另外,它还会从文本框中删除 DiaCritics。我使用 Javascript 函数来完成此操作。onchange
事件在输入标记上触发。因此,在此函数中,我将带有 javascript 函数名称的 onchange 事件添加到 htmlAttributes 列表中。如果我使用 IDictionary 那很容易,但是当我使用对象时就会困难得多。因此,当您开始项目时,重要的是要认识到 Html Helpers 必须服务的目的。就我而言,因为我认识到我的项目的重要性,所以我在项目中的任何地方都使用 IDictionary。
The
IDictionary<string, object>
is there for a special reason. If you want to adjust the htmlAttributes parameter in a extended function then this is possible. I have several extensions of theHtml.TextBox
function. I have for example a function thats called:TextBoxOrDisplayForNoDiaCritics
. This function enables a TextBox or displays a disabled textbox. Additional it also removes DiaCritics from the textbox. I do this with a Javascript function. The eventonchange
fires on the input tag. So in this function I add that onchange event with the javascript function name to the htmlAttributes list. If I use a IDictionary thats easy but when I use an object that will be much harder.So when you start your project its important to recognize what purpose your Html Helpers have to serve. In my case because I recognize the importance in my project I use IDictionary everywhere in my Project.
克里斯回答了所有问题。
我给出了使用 IDictionary 的更多理由:在 MVC 3.0 之前,当您需要像“data-something”这样的 HTML5 属性时,您不能使用匿名对象:D
干杯
Chris answers all the thing.
I give 1 more reason why use IDictionary: Prior to MVC 3.0, you could not use anonymous object when you need a HTML5 attribute like "data-something" :D
Cheers