Html.Hidden 和 Html.HiddenFor 有什么区别

发布于 2024-10-06 09:05:36 字数 103 浏览 1 评论 0原文

我可以在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

谷夏 2024-10-13 09:05:36

大多数 MVC 帮助器方法都有 XXXFor 变体。它们旨在与具体模型类结合使用。这个想法是允许帮助程序根据您在 lambda 中指定的属性为表单输入控件派生适当的“名称”属性。这意味着您可以消除“魔术字符串”,否则您必须使用这些“魔术字符串”来将模型属性与视图相关联。例如:

Html.Hidden("Name", "Value")

将导致:

<input id="Name" name="Name" type="hidden" value="Value">

在您的控制器中,您可能有如下操作:

[HttpPost]
public ActionResult MyAction(MyModel model) 
{
}

和模型如下:

public class MyModel 
{
    public string Name { get; set; }
}

我们上面使用的原始 Html.Hidden 将与 Name 模型中的属性。但是,必须使用字符串(“Name”)指定属性的值“Name”,这有点令人反感。如果您重命名模型上的 Name 属性,您的代码将会中断,并且错误将有些难以找出。另一方面,如果您使用 HiddenFor,您将受到保护:

Html.HiddenFor(x => x.Name, "Value");

现在,如果您重命名 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:

Html.Hidden("Name", "Value")

Will result in:

<input id="Name" name="Name" type="hidden" value="Value">

In your controller, you might have an action like:

[HttpPost]
public ActionResult MyAction(MyModel model) 
{
}

And a model like:

public class MyModel 
{
    public string Name { get; set; }
}

The raw Html.Hidden we used above will get correlated to the Name 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 the Name property on the Model, your code will break and the error will be somewhat difficult to figure out. On the other hand, if you use HiddenFor, you get protected from that:

Html.HiddenFor(x => x.Name, "Value");

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 typing x..

雪落纷纷 2024-10-13 09:05:36

Html.Hidden 创建隐藏输入,但您必须指定名称以及要赋予该字段和值的所有属性。 Html.HiddenFor 为您传递给它的对象创建一个隐藏输入,它们看起来像这样:

Html.Hidden("yourProperty",model.yourProperty);

Html.HiddenFor(m => m.yourProperty)

在这种情况下,输出是相同的!

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:

Html.Hidden("yourProperty",model.yourProperty);

Html.HiddenFor(m => m.yourProperty)

In this case the output is the same!

眼眸里的快感 2024-10-13 09:05:36

HtmlHelper 类中的每个方法都有一个带有 For 后缀的孪生方法。
Html.Hidden 接受一个字符串作为您必须提供的参数,但 Html.HiddenFor 接受一个 Expression,如果您认为该表达式是 强类型视图您可以从中受益,并

o=>o.SomeProperty 

在使用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

o=>o.SomeProperty 

instead of "SomeProperty" in the case of using Html.Hidden method.

若水般的淡然安静女子 2024-10-13 09:05:36

Html.Hidden 和 Html.HiddenFor 用于生成由控制器中的操作方法等待的名称-值对。
示例用法(*):

@using (Html.BeginForm("RemoveFromCart", "Cart")) {
                    @Html.Hidden("ProductId", line.Product.ProductID)
                    @Html.HiddenFor(x => x.ReturnUrl)
                    <input class="btn btn-sm btn-warning"
                           type="submit" value="Remove" />
                }

如果您的操作方法等待“ProductId”,您必须通过使用(Html.Hidden 或 Html.HiddenFor)在表单中生成此名称
对于无法使用强类型模型生成此名称的情况,您只需使用“ProductId”字符串编写此名称即可。

public ViewResult RemoveFromCart(int productId, string returnUrl){...}

如果我编写了 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(*):

@using (Html.BeginForm("RemoveFromCart", "Cart")) {
                    @Html.Hidden("ProductId", line.Product.ProductID)
                    @Html.HiddenFor(x => x.ReturnUrl)
                    <input class="btn btn-sm btn-warning"
                           type="submit" value="Remove" />
                }

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".

public ViewResult RemoveFromCart(int productId, string returnUrl){...}

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)

蝶…霜飞 2024-10-13 09:05:36

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.

毁梦 2024-10-13 09:05:36

我想在已经解释的内容的基础上添加更多信息。

  • 主要区别在于 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, Object, Object)  
  • 使用指定的 HTML 帮助器、表单字段的名称、值和 HTML 属性返回隐藏的输入元素。
Hidden(HtmlHelper, String, Object, IDictionary<String,Object>)
  • 使用指定的 HTML 帮助器、表单字段的名称、值和 HTML 属性返回隐藏的输入元素。
Hidden(HtmlHelper, String, Object)
  • 使用指定的 HTML 帮助器、表单字段的名称和值返回隐藏的输入元素。
    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

HiddenFor<TModel,TProperty>(HtmlHelper<TModel>, Expression<Func<TModel,TProperty>>)
  • 返回 HTML对象中由指定表达式表示的每个属性的隐藏输入元素。
HiddenFor<TModel,TProperty>
    (HtmlHelper<TModel>, 
     Expression<Func<TModel,TProperty>>,
     IDictionary<String,Object>)    
  • 使用指定的 HTML 属性,为指定表达式表示的对象中的每个属性返回 HTML 隐藏输入元素。
HiddenFor<TModel,TProperty>(HtmlHelper<TModel>, Expression<Func<TModel,TProperty>>, Object)
  • 使用指定的 HTML 属性,为指定表达式表示的对象中的每个属性返回 HTML 隐藏输入元素。

其中:

  • TModel:模型的类型。
  • TProperty:属性的类型。
  • 表达式:标识包含要呈现的属性的对象的表达式。

I want to add additional information to what has been already explained.

  • The main difference is that HiddenFor is strongly typed, while Hidden is NOT.
  • Both are found at: System.Web.Mvc.Html
  • Both methods return: 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, Object, Object)  
  • Returns a hidden input element by using the specified HTML helper, the name of the form field, the value, and the HTML attributes.
Hidden(HtmlHelper, String, Object, IDictionary<String,Object>)
  • Returns a hidden input element by using the specified HTML helper, the name of the form field, the value, and the HTML attributes.
Hidden(HtmlHelper, String, Object)
  • Returns a hidden input element by using the specified HTML helper, the name of the form field, and the value.
    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

HiddenFor<TModel,TProperty>(HtmlHelper<TModel>, Expression<Func<TModel,TProperty>>)
  • Returns an HTML hidden input element for each property in the object that is represented by the specified expression.
HiddenFor<TModel,TProperty>
    (HtmlHelper<TModel>, 
     Expression<Func<TModel,TProperty>>,
     IDictionary<String,Object>)    
  • Returns an HTML hidden input element for each property in the object that is represented by the specified expression, using the specified HTML attributes.
HiddenFor<TModel,TProperty>(HtmlHelper<TModel>, Expression<Func<TModel,TProperty>>, Object)
  • Returns an HTML hidden input element for each property in the object that is represented by the specified expression, using the specified HTML attributes.

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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文