2013-10-31 3 views
25

내가 가지고있는 내 asp.net MVC 웹 응용 프로그램 내에서 다음 -만드는 방법에 @ Html.EditorFor 장애인

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

하지만 필드가 비활성화되지 않습니다 ,, 할 수있는 사람 adivce하세요? THanks

+12

을 이후 '@ Html.EditorFor (model => model.Property, new {htmlAttributes = new {@ disabled = "disabled"}})' –

+0

당신이 ASP 코어를 사용한다면, 여기에 해결책을 설명했다 : https : // stackoverflow. co.kr/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

다른 대안 : 사업부 또는 특정 ID와 스팬으로 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> 
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() 예를 들어 사용할 수 있습니다 시도 할 수 있습니다 : - MVC 5.1에서 @Html.EditorFor(model => model.Nama, new { htmlAttributes = new { @class = "form-control", @disabled ="true" } })