asp.net-mvc 如何在 MVC3 中创建隐藏字段?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5732044/
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
How do I create a hidden field in MVC3?
提问by JonathanLaker
I'm using MVC3. When I make a field with
我正在使用 MVC3。当我用
@Html.EditorFor(model => model.RowKey)
Then the value gets sent back to the controller which is what I need. However I don't want the field to be visible even though I want its value to be returned to the controller.
然后将值发送回控制器,这正是我需要的。但是,即使我希望将其值返回给控制器,我也不希望该字段可见。
Is there a way I can make a field hidden with MVC3?
有没有办法让 MVC3 隐藏一个字段?
回答by moi_meme
@Html.HiddenFor(model => model.RowKey)
see What does Html.HiddenFor do?for an example
请参阅Html.HiddenFor 做什么?举个例子
回答by Major Productions
If you want to keep Html.EditorFor()
, you could always mark a particular property of your model as hidden:
如果您想保留Html.EditorFor()
,您始终可以将模型的特定属性标记为隐藏:
using System.Web.Mvc.HiddenInput;
public class Model
{
[HiddenInput(DisplayValue = false)]
// some property
}
回答by Aidan
This works for me:
这对我有用:
@Html.EditorFor(m => m.RowKey, new { htmlAttributes = new { @style="display: none" } })
回答by Robban
Could you use the hiddenfor helper, like so:
你能不能使用 hiddenfor 助手,像这样:
@Html.HiddenFor(m => m.RowKey)