asp.net-mvc-3html.textbox

MVC 3 html.TextBoxFor readonly Properties to be set dynamically


    <div class="editor-label" style="position:static">
        <%: Html.LabelFor(Model => Model.Date, "Date")%>
        <span style="padding-left:52px">
            <%: Html.TextBoxFor(Model => Model.Date)%>

Here i want to set the readonly property to the textbox on a button click/Action Click. how do i do it. At the initial page the data is been displayed as readonly and on the edit button click. This should be changed to editable textbox.I have almost seven textboxes i need to do the same for all.


Solution

  • If I understand your question correctly, this can be accomplished with a little javascript. Here's a simple example.

    Render each textbox as readonly on page load:

    <%: Html.TextBoxFor(model => model.Date, new { @readonly = "readonly" }) %>
    

    Add a click handler for your edit button that will remove the readonly attribute from each textbox:

    $("#editButton").click(function() {
        $("#Date").removeAttr("readonly");
    });