asp.net-mvc 如何禁用@Html.EditorFor
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19703753/
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 the @Html.EditorFor Disabled
提问by john Gu
I have the following inside my asp.net mvc web application :-
我的 asp.net mvc web 应用程序中有以下内容:-
<div><span class="f">Data Center Name</span> @Html.EditorFor(model => model.Switch.TMSRack.DataCenter.Name, new { disabled = "disabled" })</div>
but the field will not be disabled ,, can anyone adivce please? THanks
但该领域不会被禁用,任何人都可以请教吗?谢谢
回答by Kaf
@Html.EditorFor()does not have an overload to support htmlAttributes. You could try @Html.TextBoxFor()
@Html.EditorFor()没有支持 htmlAttributes 的重载。你可以试试@Html.TextBoxFor()
@Html.TextBoxFor(model => model.propertyName, new {disabled= "disabled" })
If you are using system key words such as classin htmlAttributes please add @before the attribute name.
如果您class在 htmlAttributes中使用系统关键字,请@在属性名称前添加。
Ex:
前任:
@Html.TextBoxFor(model => model.propertyName, new {@class = "disabledClass" })
回答by SaadK
Using MVC 5, @Html.EditorFor()supports htmlAttributes
使用 MVC 5,@Html.EditorFor()支持 htmlAttributes
@Html.EditorFor(model => model.x, new { htmlAttributes = new { @class = "form-control", @disabled = "disabled" } })
The above example shows how you can add a class and also the disabled attribute
上面的示例显示了如何添加类以及禁用属性
回答by Sava B.
Another alternative: surround the EditorFor with a div or a span with a certain id, and then use a bit of jquery/js:
另一种选择:用具有特定 id 的 div 或 span 包围 EditorFor,然后使用一些 jquery/js:
<span id="editors">
@Html.EditorFor(x => x.ViewModelProperty)
</span>
<!-- Disable above editors. EditorFor doesn't allow html attributes unfortunately. -->
<script type="text/javascript">
$(function () { $('#editors :input').attr("disabled", true); });
</script>
回答by remajabioinformatik
You can also use EditorFor()eg:
您还可以使用EditorFor()例如:
@Html.EditorFor(model => model.Nama, new { htmlAttributes = new { @disabled ="true", @class = "form-control" } })
回答by Armando Baltazar
If you lose information when you accept the Edit-changes ... You could try
如果您在接受编辑更改时丢失信息......您可以尝试
<h3>@Model.x</h3>
@Html.HiddenFor(model => model.x, new { htmlAttributes = new { @class = "form-control" } })

