2013-10-31 8 views
25

Ich habe folgende in meiner asp.net MVC Web-Anwendung: -Wie macht man die @ Html.EditorFor Behinderte

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

aber das Feld wird nicht deaktiviert wird ,, kann jemand adivce bitte? THanks

+12

Von MVC 5.1 ab '@ Html.EditorFor (model => model.Property, neu {htmlAttributes = new {@ disabled =" disabled "}})' –

+0

Wenn Sie asp core verwenden, habe ich hier eine Lösung erklärt: https: // stackoverflow. com/a/44639822/6339469 – HamedH

Antwort

46

@Html.EditorFor() hat keine Überladung, um htmlAttributes zu unterstützen. Sie könnten @Html.TextBoxFor()

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

versuchen Wenn Sie System Schlüsselwörter verwenden wie class in Htmlattributes bitte @ vor dem Attributnamen hinzufügen.

Ex:

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

arbeitete mit beiden c Lass und TextBoxFor: @ Html.TextBoxFor (Modell => model.Doctor.Label1, neue {@class = "Formularsteuerung", deaktiviert = "deaktiviert"}) – MuniR

6

Eine weitere Alternative: umgibt die EditorFor mit einem div oder einer Spanne mit einem bestimmten ID und dann ein bisschen jquery/js verwenden:

<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

Mit MVC 5 , @Html.EditorFor() unterstützt Htmlattributes

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

Das obige Beispiel zeigt, wie Sie eine Klasse hinzufügen und al so das untaugliche Attribut

+1

nicht mit mir arbeiten. Ich verwende MVC 5 – MuniR

+0

Wie kann ich das deaktivierte Attribut bedingt machen? –

+0

Sie können jQuery verwenden, um das deaktivierte Attribut mit if -Anweisungen $ ('. InputDisabled') hinzuzufügen und zu entfernen. Prop ("disabled", false); $ ('. InputDisabled'). Prop ("deaktiviert", true); – SaadK

0

Wenn Sie Informationen verlieren, wenn Sie die Bearbeiten-Änderungen zu übernehmen ... könnten Sie versuchen,

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

Sie auch EditorFor() können zB: - @Html.EditorFor(model => model.Nama, new { htmlAttributes = new { @class = "form-control", @disabled ="true" } })