2013-10-31 69 views
25

我有以下我的asp.net MVC Web应用程序中: -如何使@ Html.EditorFor残疾人

<div><span class="f">Data Center Name</span> @Html.EditorFor(model => model.Switch.TMSRack.DataCenter.Name, new { disabled = "disabled" })</div> 

,但该领域不会被禁用,,谁能书于好吗? THanks

+12

从MVC 5.1起'@ H​​tml.EditorFor(model => model.Property,new {htmlAttributes = new {@ disabled =“disabled”}})' –

+0

如果您使用的是asp核心,我在这里解释了一个解决方案: https:// stackoverflow。 com/a/44639822/6339469 – HamedH

回答

46

@Html.EditorFor()没有重载支持htmlAttributes。如果使用的是系统的关键字,如htmlAttributes class请属性名称前面加@你可以尝试@Html.TextBoxFor()

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

例:

@Html.TextBoxFor(model => model.propertyName, new {@class = "disabledClass" }) 
+0

与两个c lass和TextBoxFor: @ Html.TextBoxFor(model => model.Doctor.Label1,new {@class =“form-control”,disabled =“disabled”}) – MuniR

6

另一种替代方法:包围EditorFor与一个div或具有一定ID的跨度,然后用一个比特的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> 
13

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

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

上面的例子说明如何添加一个类人所以残疾人属性

+1

不适用于我。我正在使用MVC 5 – MuniR

+0

如何使残疾人属性有条件? –

+0

您可以使用jQuery来添加和删除if语句的禁用属性 $('。inputDisabled')。prop(“disabled”,false); $('。inputDisabled')。prop(“disabled”,true); – SaadK

0

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

<h3>@Model.x</h3> 
@Html.HiddenFor(model => model.x, new { htmlAttributes = new { @class = "form-control" } }) 
1

您还可以使用EditorFor() 如: - @Html.EditorFor(model => model.Nama, new { htmlAttributes = new { @class = "form-control", @disabled ="true" } })