Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 1000
samdinesh Avatar asked Dec 22 '22 12:12

samdinesh


2 Answers

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");
});
like image 162
gram Avatar answered May 25 '23 12:05

gram


Use the following:

 Html.TextBoxFor(m => m.Whatever, new {id = "", @class="", 
      placeholder="" @readonly="readonly"}) 

Hope this helps :)

like image 38
Ruaan Volschenk Avatar answered May 25 '23 11:05

Ruaan Volschenk