.net Html.HiddenFor 有什么作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3866716/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 14:47:29  来源:igfitidea点击:

What does Html.HiddenFor do?

.netasp.net-mvcasp.net-mvc-2html-helperhtml.hiddenfor

提问by JPCF

Although I have read the documentation on Html.HiddenFor, I've not grasped what is it used for...

虽然我已经阅读了 Html.HiddenFor 的文档,但我还没有掌握它的用途......

Could somebody explain its uses and give a short example?

有人可以解释它的用途并举一个简短的例子吗?

Where should those helpers go in the code?

这些助手应该放在代码中的什么位置?

回答by Justin Niessner

It creates a hidden input on the form for the field (from your model) that you pass it.

它在表单上为您传递的字段(来自您的模型)创建一个隐藏输入。

It is useful for fields in your Model/ViewModel that you need to persist on the page and have passed back when another call is made but shouldn't be seen by the user.

对于模型/视图模型中的字段很有用,这些字段需要保留在页面上,并在进行另一个调用时传回,但不应被用户看到。

Consider the following ViewModel class:

考虑以下 ViewModel 类:

public class ViewModel
{
    public string Value { get; set; }
    public int Id { get; set; }
}

Now you want the edit page to store the ID but have it not be seen:

现在您希望编辑页面存储 ID 但不显示它:

<% using(Html.BeginForm() { %>
    <%= Html.HiddenFor(model.Id) %><br />
    <%= Html.TextBoxFor(model.Value) %>
<% } %>

This results in the equivalent of the following HTML:

这将导致等效于以下 HTML:

<form name="form1">
    <input type="hidden" name="Id">2</input>
    <input type="text" name="Value" value="Some Text" />
</form>

回答by James Still

And to consume the hidden ID input back on your Edit action method:

并在您的 Edit 操作方法中使用隐藏的 ID 输入:

[HttpPost]
public ActionResult Edit(FormCollection collection)
{
    ViewModel.ID = Convert.ToInt32(collection["ID"]);
}

回答by Devin Andres Salemi

Like a lot of functions, this one can be used in many different ways to solve many different problems, I think of it as yet another tool in our toolbelts.

像许多函数一样,这个函数可以以多种不同的方式使用来解决许多不同的问题,我认为它是我们工具带中的另一个工具。

So far, the discussion has focused heavily on simply hiding an ID, but that is only one value, why not use it for lots of values! That is what I am doing, I use it to load up the values in a class only one view at a time, because html.beginform creates a new object and if your model object for that view already had some values passed to it, those values will be lost unless you provide a reference to those values in the beginform.

到目前为止,讨论主要集中在简单地隐藏一个 ID,但这只是一个值,为什么不将它用于很多值!这就是我正在做的事情,我使用它一次只加载一个视图中的值,因为 html.beginform 创建了一个新对象,如果该视图的模型对象已经有一些值传递给它,那些除非您在 beginform 中提供对这些值的引用,否则这些值将丢失。

To see a great motivation for the html.hiddenfor, I recommend you see Passing data from a View to a Controller in .NET MVC - "@model" not highlighting

要查看 html.hiddenfor 的强大动机,我建议您查看将数据从视图传递到 .NET MVC 中的控制器 - “@model”不突出显示

回答by Abi

The Use of Razor code @Html.Hidden or @Html.HiddenFor is similar to the following Html code

Razor代码@Html.Hidden或@Html.HiddenFor的使用类似于下面的Html代码

 <input type="hidden"/>

And also refer the following link

并参考以下链接

https://msdn.microsoft.com/en-us/library/system.web.mvc.html.inputextensions.hiddenfor(v=vs.118).aspx

https://msdn.microsoft.com/en-us/library/system.web.mvc.html.inputextensions.hiddenfor(v=vs.118).aspx