40

I have the following inside my asp.net mvc web application :-

<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

4

6 回答 6

67

@Html.EditorFor()没有支持 htmlAttributes 的重载。你可以试试@Html.TextBoxFor()

@Html.TextBoxFor(model => model.propertyName, new {disabled= "disabled" })

如果您使用系统关键字,例如class在 htmlAttributes 中,请@在属性名称前添加。

前任:

@Html.TextBoxFor(model => model.propertyName, new {@class = "disabledClass" })
于 2013-10-31T10:21:48.153 回答
41

使用 MVC 5,@Html.EditorFor()支持 htmlAttributes

@Html.EditorFor(model => model.x, new { htmlAttributes = new { @class = "form-control", @disabled = "disabled" } })

上面的示例显示了如何添加一个类以及 disabled 属性

于 2016-10-18T08:48:11.227 回答
9

另一种选择:用具有特定 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>
于 2015-01-16T17:41:55.613 回答
4

对于那些丢失文本值的人:禁用字段不会将其值传递给控制器​​。相反,使用@readonly = "readonly"

@Html.EditorFor(model => model.EmployeeCode, new { htmlAttributes = new { @readonly = "readonly", @class = "form-control"} })
于 2020-11-06T07:18:50.143 回答
3

如果您在接受编辑更改时丢失信息...您可以尝试

<h3>@Model.x</h3>
@Html.HiddenFor(model => model.x, new { htmlAttributes = new { @class = "form-control" } })
于 2017-04-19T23:26:28.107 回答
3

您还可以使用EditorFor() 例如:

@Html.EditorFor(model => model.Nama, new { htmlAttributes = new { @disabled ="true", @class = "form-control" } })
于 2018-02-08T15:36:25.600 回答