如何使@ Html.EditorFor不可见? [英] How to make @Html.EditorFor invisible?
问题描述
当我只删除我不希望用户在View中看到的编辑器字段时,他们的数据变为null,我想保留数据,但是,我不想用户编辑它.
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.
<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>
我所指的@ Html.EditorFor是model.parent_directory.谁能提出不涉及js的解决方案?我尝试使用DisplayFor而不是EditorFor,但仍然得到一个空值.
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.
推荐答案
请查看此MSDN页面. http://msdn.microsoft.com/zh-CN/library/system.web.mvc.html.editorextensions.editorfor(v=vs.118).aspx
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
根据该页面,出于明显的原因,您不能提供html属性作为参数.因为EditorFor
可以是input
字段或select
,并且它们的html属性彼此不同.
According to that page, you can't provide html attributes as a parameter for obvious reasons. Because EditorFor
can be an input
field or select
and their html attributes differs from one another.
如果您只想暂时隐藏它,并稍后再显示给用户,则可以使用@Html.TextBoxFor
.您可以在
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 TextBoxFor
overloads at http://msdn.microsoft.com/en-us/library/system.web.mvc.html.inputextensions.textboxfor(v=vs.118).aspx
例如:
@Html.TextBoxFor(model => model.Name, new {style = 'display:none'})
如果要在整个过程中隐藏这些详细信息,可以使用@Html.HiddenFor
if you want to keep these details hidden throughout the process, you can use @Html.HiddenFor
例如
@Html.HiddenFor(model => model.Name)
这篇关于如何使@ Html.EditorFor不可见?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!