Html.HiddenFor 是做什么的?
虽然我已经阅读了 Html.HiddenFor 的文档,但我还没有掌握它的用途......
有人可以解释它的用途并给出一个简短的例子吗?
这些助手应该放在代码中的哪里?
Although I have read the documentation on Html.HiddenFor, I've not grasped what is it used for...
Could somebody explain its uses and give a short example?
Where should those helpers go in the code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
它在表单上为您传递的字段(来自模型)创建隐藏输入。
对于模型/视图模型中需要保留在页面上并在进行另一个调用时传回但用户不应该看到的字段非常有用。
考虑以下 ViewModel 类:
现在您希望编辑页面存储 ID,但不让其可见:
这将产生与以下 HTML 等效的结果:
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:
Now you want the edit page to store the ID but have it not be seen:
This results in the equivalent of the following HTML:
并在您的编辑操作方法中使用隐藏的 ID 输入:
And to consume the hidden ID input back on your Edit action method:
与许多功能一样,这个功能可以以多种不同的方式使用来解决许多不同的问题,我认为它是我们工具带中的另一种工具。
到目前为止,讨论主要集中在简单地隐藏 ID,但这只是一个值,为什么不将它用于许多值!这就是我正在做的事情,我使用它一次只加载一个类中的值,因为 html.beginform 创建一个新对象,并且如果该视图的模型对象已经传递了一些值,那么这些值除非您在开始表单中提供对这些值的引用,否则这些值将会丢失。
要了解 html.hiddenfor 的巨大动机,我建议您查看 在 .NET MVC 中将数据从视图传递到控制器 - “@model”不突出显示
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.
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
Razor 代码 @Html.Hidden 或 @Html.HiddenFor 的使用类似于以下 Html 代码
,另请参阅以下链接
https://msdn.microsoft.com/en-us/library/system.web.mvc.html。 inputextensions.hiddenfor(v=vs.118).aspx
The Use of Razor code @Html.Hidden or @Html.HiddenFor is similar to the following Html code
And also refer the following link
https://msdn.microsoft.com/en-us/library/system.web.mvc.html.inputextensions.hiddenfor(v=vs.118).aspx