4
    <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.

samdinesh
  • 147
  • 1
  • 3
  • 9

3 Answers3

10

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");
});
gram
  • 2,772
  • 20
  • 24
  • Thank you for your reply but it doesn't work.I tried a lot with other options but javascript doesn't work at all with the MVC controls. $(document).ready(function(){ $("#datebox").attr('@readonly',' readonly'); });
    <%: Html.LabelFor(Model => Model.Date, "Date")%> <%: Html.TextBoxFor(Model => Model.Date, new { @class = "textboxes",@id = "datebox")%>
    – samdinesh Jun 24 '11 at 02:01
  • 2
    Change your javascript call to this: $("#datebox").attr('readonly',' readonly'); Also, in your Date textbox, you don't need to use @id, it can just be id. Leave @class as it is. The '@' is for using C# reserved words as variable names. – gram Jun 24 '11 at 11:59
1

Use the following:

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

Hope this helps :)

David
  • 15,894
  • 22
  • 55
  • 66
Ruaan Volschenk
  • 717
  • 2
  • 11
  • 23
0

I think you should put

@readonly = true

or

@readonly = *your-expression-here*
Junior Mayhé
  • 16,144
  • 26
  • 115
  • 161