asp.net-mvc 如何使@Html.EditorFor 不可见?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26842734/
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 to make @Html.EditorFor invisible?
提问by Dave Demirkhanyan
When I am just deleting the editor fields that I dont' want the user to see in the View , their data becomes null, I want to keep the data, but ,I don't want the user to edit it.
当我只是删除我不希望用户在 View 中看到的编辑器字段时,他们的数据变为空,我想保留数据,但是,我不希望用户编辑它。
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.parent_directory)
</div>
<div class="editor-field">
@Html.DisplayFor(model => model.parent_directory)
</div>
<p>
<input type="submit" value="Add" />
</p>
The @Html.EditorFor I am referring to is model.parent_directory. Can anyone suggest a solution that doesn't involve js ? I tried using DisplayFor instead of EditorFor and I still get a null value.
我指的@Html.EditorFor 是model.parent_directory。任何人都可以提出一个不涉及 js 的解决方案吗?我尝试使用 DisplayFor 而不是 EditorFor ,但仍然得到一个空值。
回答by Amila
Please take a look at this MSDN page. http://msdn.microsoft.com/en-us/library/system.web.mvc.html.editorextensions.editorfor(v=vs.118).aspx
请查看此 MSDN 页面。 http://msdn.microsoft.com/en-us/library/system.web.mvc.html.editorextensions.editorfor(v=vs.118).aspx
According to that page, you can't provide html attributes as a parameter for obvious reasons. Because EditorForcan be an inputfield or selectand their html attributes differs from one another.
根据该页面,由于显而易见的原因,您不能提供 html 属性作为参数。因为EditorFor可以是一个input字段 orselect并且它们的 html 属性彼此不同。
If you just want to hide it temporarily and show it later to the user, you can use @Html.TextBoxFor. You can find out more about TextBoxForoverloads at http://msdn.microsoft.com/en-us/library/system.web.mvc.html.inputextensions.textboxfor(v=vs.118).aspx
如果您只是想暂时隐藏它并稍后向用户显示,您可以使用@Html.TextBoxFor. 您可以TextBoxFor在http://msdn.microsoft.com/en-us/library/system.web.mvc.html.inputextensions.textboxfor(v=vs.118).aspx 上找到有关重载的更多信息
e.g:
例如:
@Html.TextBoxFor(model => model.Name, new {style = 'display:none'})
if you want to keep these details hidden throughout the process, you can use @Html.HiddenFor
如果你想在整个过程中隐藏这些细节,你可以使用 @Html.HiddenFor
e.g
例如
@Html.HiddenFor(model => model.Name)
回答by Pavel
Use hidden fields for this:
为此使用隐藏字段:
@Html.HiddenFor(model => model.Name)
回答by tarzanbappa
If you want to display the fields and don't allow users to edit, you can make them read only..
如果您想显示字段并且不允许用户编辑,您可以将它们设为只读。
@Html.EditorFor(model => model.Name, new {@readonly = "readonly" })
If you don't want to show the fields to user and just need to hold data you can use
如果您不想向用户显示字段而只需要保存可以使用的数据
@Html.HiddenFor(model => model.Name)
回答by RafiO
You can do this with:
你可以这样做:
@Html.Editor("WContractID", new { htmlAttributes = new { @class = "form-control", @style = "display: none" } })

