Jibin Balachandran
Jibin Balachandran

Reputation: 3441

How to create a disabled textbox in MVC razor view

I want to create text area which will be disabled or invisible at starting. There's a dropdown list, on selection of last option i.e other, it should enable or make the text area visible and take the value and pass to controller on submission.

Upvotes: 1

Views: 6757

Answers (3)

sushil chaurasia
sushil chaurasia

Reputation: 1

You should try this for creating disabled textArea

@Html.TextAreaFor(m => m.property, new { id = "txtId", disabled = "disabled" })

Upvotes: 0

Suren Srapyan
Suren Srapyan

Reputation: 68655

Use

@Html.TextArea("Name", null, new { disabled="disabled" })

For You:

<div> 
        @Html.TextArea("Name", null, new { disabled="true" })
        @Html.DropDownList("switch", new List<SelectListItem>() { 
    new SelectListItem(){Text="Enable", Value="Enable"},
    new SelectListItem(){Text="Disable", Value="Disable", Selected=true},
   })
    </div>

    <script>

 $(function ($) {

        var dropDown = $("#switch");
        var txtName = $("#Name");

        dropDown.change(function () {
            if (dropDown.val() === "Disable")
            {
                txtName.attr("disabled", true);
            }
            else {
                txtName.removeAttr("disabled");
            }
        })


    })(jQuery)



    </script>

Upvotes: 6

dlght
dlght

Reputation: 926

You should hide the text area, not only disable it. What you can do is add the html :

@Html.TextArea("Text", null, new { @class="hiddenOptionArea", disabled="disabled" })

and apply the class:

hiddenOptionArea {
    display:none
}

Also you better use @Html.EditorFor and make yourself a template for your model, so that you can reuse your html wherever you need it - you can find the link here: http://rachelappel.com/razor/partial-views-in-asp-net-mvc-3-w-the-razor-view-engine/

Upvotes: 0

Related Questions