这是什么Html.HiddenFor办?这是什么、Html、HiddenFor

2023-09-02 01:35:57 作者:烈酒烫心

虽然我已阅读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?

应该在哪里这些佣工在code去了?

Where should those helpers go in the code?

推荐答案

创建表单字段上的一个隐藏的输入(从你的模型),你传递给它。

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 modified 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 editable seen:

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

这导致以下HTML的当量:

Which 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>