ASP.NET MVC中htmlAttributes的匿名类和IDictionary 之间的区别?

时间:2022-02-18 19:01:25

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:

例如,如果检查这两个扩展方法,唯一的区别是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);

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.

我检查了MVC源代码,我知道在后台他们使用相同的方法,但是接受匿名对象的那个使用HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)来使匿名对象成为字典。

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?

在我看来,使用匿名对象的视图更清晰。你们觉得怎么样?使用匿名对象有什么缺点吗?

3 个解决方案

#1


10  

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.

没有太多区别,但是使用匿名对象对于调用者来说是更清晰且更易读的语法,现在被认为是更标准的实践。如果您是微优化的粉丝,那么使用IDictionary可能会有轻微的,可忽略的性能优势。

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.

自从ASP.NET MVC 1.0 CTP时代以来,IDictionary重载选项可能已经停滞不前,而C#3.0和匿名对象仍然很新。 Eilon Lipton的博客文章提议使用C#3.0匿名类型作为词典提供了一些背景知识。

#2


2  

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 the Html.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 event onchange 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.

IDictionary 是出于特殊原因。如果要在扩展函数中调整htmlAttributes参数,则可以进行此操作。我有几个Html.TextBox函数的扩展。我有一个名为TextBoxOrDisplayForNoDiaCritics的函数。此功能启用TextBox或显示禁用的文本框。此外,它还从文本框中删除DiaCritics。我使用Javascript函数执行此操作。事件onchange在输入标记上触发。所以在这个函数中,我将带有javascript函数名称的onchange事件添加到htmlAttributes列表中。如果我使用IDictionary很容易,但是当我使用一个更难的对象时。 ,object>

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.

所以当你开始你的项目时,重要的是要认识到你的Html助手必须服务的目的。在我的情况下,因为我认识到我的项目中的重要性,我在我的项目中使用IDictionary。

#3


2  

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

我给出了另外一个使用IDictionary的原因:在MVC 3.0之前,当你需要像“data-something”这样的HTML5属性时,你无法使用匿名对象:D

Cheers

#1


10  

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.

没有太多区别,但是使用匿名对象对于调用者来说是更清晰且更易读的语法,现在被认为是更标准的实践。如果您是微优化的粉丝,那么使用IDictionary可能会有轻微的,可忽略的性能优势。

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.

自从ASP.NET MVC 1.0 CTP时代以来,IDictionary重载选项可能已经停滞不前,而C#3.0和匿名对象仍然很新。 Eilon Lipton的博客文章提议使用C#3.0匿名类型作为词典提供了一些背景知识。

#2


2  

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 the Html.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 event onchange 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.

IDictionary 是出于特殊原因。如果要在扩展函数中调整htmlAttributes参数,则可以进行此操作。我有几个Html.TextBox函数的扩展。我有一个名为TextBoxOrDisplayForNoDiaCritics的函数。此功能启用TextBox或显示禁用的文本框。此外,它还从文本框中删除DiaCritics。我使用Javascript函数执行此操作。事件onchange在输入标记上触发。所以在这个函数中,我将带有javascript函数名称的onchange事件添加到htmlAttributes列表中。如果我使用IDictionary很容易,但是当我使用一个更难的对象时。 ,object>

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.

所以当你开始你的项目时,重要的是要认识到你的Html助手必须服务的目的。在我的情况下,因为我认识到我的项目中的重要性,我在我的项目中使用IDictionary。

#3


2  

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

我给出了另外一个使用IDictionary的原因:在MVC 3.0之前,当你需要像“data-something”这样的HTML5属性时,你无法使用匿名对象:D

Cheers