Html.HiddenFor 有什么作用? [英] What does Html.HiddenFor do?

查看:33
本文介绍了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?

推荐答案

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

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.

考虑以下 ViewModel 类:

Consider the following ViewModel class:

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

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

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

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

这相当于以下 HTML:

This results in the equivalent of the following HTML:

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

这篇关于Html.HiddenFor 有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆