Html.Hidden 和 Html.HiddenFor 有什么区别
我可以在 MSDN 上找到 Html.HiddenFor 的良好定义,但我在 Html.Hidden 上能找到的唯一内容与它存在的问题有关。
有人能给我一个很好的定义和例子吗?
I can find a good definition for Html.HiddenFor on MSDN but the only thing I can find on Html.Hidden is related to problems it has.
Can someone give me a good definition and an example.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
大多数 MVC 帮助器方法都有 XXXFor 变体。它们旨在与具体模型类结合使用。这个想法是允许帮助程序根据您在 lambda 中指定的属性为表单输入控件派生适当的“名称”属性。这意味着您可以消除“魔术字符串”,否则您必须使用这些“魔术字符串”来将模型属性与视图相关联。例如:
将导致:
在您的控制器中,您可能有如下操作:
和模型如下:
我们上面使用的原始
Html.Hidden
将与Name
模型中的属性。但是,必须使用字符串(“Name”)指定属性的值“Name”,这有点令人反感。如果您重命名模型上的Name
属性,您的代码将会中断,并且错误将有些难以找出。另一方面,如果您使用HiddenFor
,您将受到保护:现在,如果您重命名
Name
属性,您将收到一个显式的运行时错误,表明该属性找不到。此外,您还可以获得静态分析的其他好处,例如在输入x.
后获得成员的下拉列表。Most of the MVC helper methods have a XXXFor variant. They are intended to be used in conjunction with a concrete model class. The idea is to allow the helper to derive the appropriate "name" attribute for the form-input control based on the property you specify in the lambda. This means that you get to eliminate "magic strings" that you would otherwise have to employ to correlate the model properties with your views. For example:
Will result in:
In your controller, you might have an action like:
And a model like:
The raw
Html.Hidden
we used above will get correlated to theName
property in the model. However, it's somewhat distasteful that the value "Name" for the property must be specified using a string ("Name"). If you rename theName
property on the Model, your code will break and the error will be somewhat difficult to figure out. On the other hand, if you useHiddenFor
, you get protected from that:Now, if you rename the
Name
property, you will get an explicit runtime error indicating that the property can't be found. In addition, you get other benefits of static analysis, such as getting a drop-down of the members after typingx.
.Html.Hidden 创建隐藏输入,但您必须指定名称以及要赋予该字段和值的所有属性。
Html.HiddenFor
为您传递给它的对象创建一个隐藏输入,它们看起来像这样:在这种情况下,输出是相同的!
The Html.Hidden creates a hidden input but you have to specify the name and all the attributes you want to give that field and value. The
Html.HiddenFor
creates a hidden input for the object that you pass to it, they look like this:In this case the output is the same!
HtmlHelper 类中的每个方法都有一个带有
For
后缀的孪生方法。Html.Hidden 接受一个字符串作为您必须提供的参数,但 Html.HiddenFor 接受一个 Expression,如果您认为该表达式是 强类型视图您可以从中受益,并
在使用Html.Hidden方法的情况下向该方法提供一个lambda表达式,而不是“SomeProperty”。
Every method in HtmlHelper class has a twin with
For
suffix.Html.Hidden takes a string as an argument that you must provide but Html.HiddenFor takes an Expression that if you view is a strongly typed view you can benefit from this and feed that method a lambda expression like this
instead of "SomeProperty" in the case of using Html.Hidden method.
Html.Hidden 和 Html.HiddenFor 用于生成由控制器中的操作方法等待的名称-值对。
示例用法(*):
如果您的操作方法等待“ProductId”,您必须通过使用(Html.Hidden 或 Html.HiddenFor)在表单中生成此名称
对于无法使用强类型模型生成此名称的情况,您只需使用“ProductId”字符串编写此名称即可。
如果我编写了 Html.HiddenFor(x => line.Product.ProductID),则帮助器将呈现一个名为“line.Product.ProductID”的隐藏字段。
该字段的名称与等待“ProductId”名称的“RemoveFromCart”操作方法的参数名称不匹配。这将阻止默认模型绑定器工作,因此 MVC 框架将无法调用该方法。
*Adam Freeman(Apress - Pro ASP.Net MVC 5)
Html.Hidden and Html.HiddenFor used to generate name-value pairs which waited by action method in controller.
Sample Usage(*):
If your action method wait for "ProductId" you have to generate this name in form via using (Html.Hidden or Html.HiddenFor)
For the case it is not possible to generate this name with strongly typed model you simple write this name with a string thats "ProductId".
If I had written Html.HiddenFor(x => line.Product.ProductID), the helper would render a hidden field with the name "line.Product.ProductID".
The name of the field would not match the names of the parameters for the "RemoveFromCart" action method which waiting the name of "ProductId". This would prevent the default model binders from working, so the MVC Framework would not be able to call the method.
*Adam Freeman (Apress - Pro ASP.Net MVC 5)
Html.Hidden('name', 'value') 创建一个名称 = 'name' 且 value = 'value' 的隐藏标记。
Html.HiddenFor(x => x.nameProp) 创建一个名称 = 'nameProp' 且值 = x.nameProp 的隐藏标记。
从表面上看,它们似乎做类似的事情,只是其中一个比另一个更方便。但它的实际价值是用于模型绑定。当 MVC 尝试将 html 与模型关联时,它需要具有属性的名称,对于 Html.Hidden,我们选择“name”,而不是“nameProp”,因此绑定不起作用。您必须有一个自定义绑定对象,或者从表单数据中获取值。如果您要重新显示页面,则必须再次将模型设置为这些值。
所以你可以使用Html.Hidden,但是如果你把名字弄错了,或者你改变了模型中的属性名称,那么当你提交表单时,自动绑定将会失败。但是通过使用类型检查表达式,您将获得代码完成,并且当您更改属性名称时,您将收到编译时错误。然后保证您在表格中填写的姓名是正确的。
MVC 更好的功能之一。
Html.Hidden('name', 'value') creates a hidden tag with name = 'name' and value = 'value'.
Html.HiddenFor(x => x.nameProp) creates a hidden tag with a name = 'nameProp' and value = x.nameProp.
At face value these appear to do similar things, with one just more convenient than the other. But its actual value is for model binding. When MVC tries to associate the html to the model, it needs to have the name of the property, and for Html.Hidden, we chose 'name', and not 'nameProp', and thus the binding wouldn't work. You'd have to have a custom binding object, or get the values from the form data. If you are redisplaying the page, you'd have to set the model to the values again.
So you can use Html.Hidden, but if you get the name wrong, or if you change the property name in the model, the auto binding will fail when you submit the form. But by using a type checked expression, you'll get code completion, and when you change the property name, you will get a compile time error. And then you are guaranteed to have the correct name in the form.
One of the better features of MVC.
我想在已经解释的内容的基础上添加更多信息。
HiddenFor
是强类型的,而Hidden
不是。System.Web.Mvc.Html
MvcHtmlString
- 类型属性设置为“隐藏”的输入元素。官方文档解释:
@Html.Hidden()
https://learn.microsoft.com/en-us/dotnet/api/system.web.mvc.html.inputextensions.hidden?view=aspnet-mvc-5.2
Hidden(HtmlHelper, String)
使用指定的 HTML 帮助器和表单字段的名称返回隐藏的输入元素。
其中:
name
:表单字段的名称和用于查找值的 ViewDataDictionary 键。value
:隐藏输入元素的值。按此顺序检索值 - ModelStateDictionary 对象、此参数的值、ViewDataDictionary 对象,最后是 html 属性中的 value 属性。@Html.HiddenFor()
https://learn.microsoft.com/en-us/dotnet/api/system.web.mvc.html.inputextensions.hiddenfor?view=aspnet-mvc-5.2
其中:
TModel
:模型的类型。TProperty
:属性的类型。表达式
:标识包含要呈现的属性的对象的表达式。I want to add additional information to what has been already explained.
HiddenFor
is strongly typed, whileHidden
is NOT.System.Web.Mvc.Html
MvcHtmlString
- An input element whose type attribute is set to "hidden".The official documentation explains:
@Html.Hidden()
https://learn.microsoft.com/en-us/dotnet/api/system.web.mvc.html.inputextensions.hidden?view=aspnet-mvc-5.2
Hidden(HtmlHelper, String)
Returns a hidden input element by using the specified HTML helper and the name of the form field.
Where:
name
: The name of the form field and the ViewDataDictionary key that is used to look up the value.value
: The value of the hidden input element. The value is retrieved in this order - the ModelStateDictionary object, the value of this parameter, the ViewDataDictionary object, and lastly, a value attribute in the html attributes.@Html.HiddenFor()
https://learn.microsoft.com/en-us/dotnet/api/system.web.mvc.html.inputextensions.hiddenfor?view=aspnet-mvc-5.2
Where:
TModel
: The type of the model.TProperty
: The type of the property.expression
: An expression that identifies the object that contains the properties to render.