这是什么Html.HiddenFor办? [英] What does Html.HiddenFor do?

查看:261
本文介绍了这是什么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?

应该在哪里这些佣工在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>

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

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