如何让@ Html.Editor为隐形?

时间:2022-09-26 20:49:22

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,我仍然得到一个空值。

4 个解决方案

#1


11  

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 EditorFor can be an input field or select and their html attributes differs from one another.

根据该页面,由于显而易见的原因,您无法提供html属性作为参数。因为EditorFor可以是输入字段或选择,并且它们的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 TextBoxFor overloads at http://msdn.microsoft.com/en-us/library/system.web.mvc.html.inputextensions.textboxfor(v=vs.118).aspx

如果您只想暂时隐藏它并稍后向用户显示,则可以使用@ Html.TextBoxFor。您可以在http://msdn.microsoft.com/en-us/library/system.web.mvc.html.inputextensions.textboxfor(v=vs.118).aspx上找到有关TextBoxFor重载的更多信息。

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)

#2


5  

Use hidden fields for this:

使用隐藏字段:

@Html.HiddenFor(model => model.Name)

#3


1  

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)

#4


1  

You can do this with:

你可以这样做:

@Html.Editor("WContractID", new { htmlAttributes = new { @class = "form-control", @style = "display: none" } })

#1


11  

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 EditorFor can be an input field or select and their html attributes differs from one another.

根据该页面,由于显而易见的原因,您无法提供html属性作为参数。因为EditorFor可以是输入字段或选择,并且它们的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 TextBoxFor overloads at http://msdn.microsoft.com/en-us/library/system.web.mvc.html.inputextensions.textboxfor(v=vs.118).aspx

如果您只想暂时隐藏它并稍后向用户显示,则可以使用@ Html.TextBoxFor。您可以在http://msdn.microsoft.com/en-us/library/system.web.mvc.html.inputextensions.textboxfor(v=vs.118).aspx上找到有关TextBoxFor重载的更多信息。

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)

#2


5  

Use hidden fields for this:

使用隐藏字段:

@Html.HiddenFor(model => model.Name)

#3


1  

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)

#4


1  

You can do this with:

你可以这样做:

@Html.Editor("WContractID", new { htmlAttributes = new { @class = "form-control", @style = "display: none" } })