2

I have three Textbox which will be in readonly mode like @Html.TextBoxFor(m => m.Whatever, new {@readonly = "readonly"}) when the view will be loaded and one Edit Button. After click on Edit button i want that Textbox to be writeable in same View. Is it possible to use same View or do i have to create another View without @readonly = "readonly" property for TextBox

Steve
  • 352
  • 2
  • 6
  • 24

2 Answers2

3

You can use javascript/jquery to remove the readonly attribute, for example

@Html.TextBoxFor(m => m.Whatever, new {@readonly = "readonly"})
<button id="edit" type="button">Edit</button>

$('#edit').click(function() {
    $('#Whatever').prop('readonly', false);
});
  • i have little bit similar question.when i click edit it pop up a screen and there i see a text box. the problem is it is not showing values on there.see my question : http://stackoverflow.com/questions/37433740/mvc-view-javascript-controller-method-javascript-div-tag n read last comment on accepted answer – Harshil Shah May 26 '16 at 07:24
  • yea i want exactly like that fiddle code. i saw your code but didn't understood very well. working on it. Thanks by d way. – Harshil Shah May 26 '16 at 07:39
  • @HarshilShah. Sorry, I accidentally deleted the comment - I'll add it back :) –  May 26 '16 at 07:43
  • @HarshilShah, On the question you linked to, its sounds like you want something like [this DotNetFiddle](https://dotnetfiddle.net/Yy78S3)? - no reason to go back to the server - and if the initial view is not displaying all properties of the model you want to edit, you can just add then into the view as data-* attributes –  May 26 '16 at 07:43
0

Down here is the TextBox and Edit button like

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

and

<button id="edit" type="button">Edit</button>

then in javascript

$('#edit').click(function() {
    $('#Whatever').removeAttr("readonly");
});

This will remove the readonly attribute from the TextBox.

anand
  • 1,559
  • 5
  • 21
  • 45
  • 1
    While using @Html.TextBoxFor(m => m.Whatever...), it automatically gives an id for textbox which is "Whatever". So you don't have to give additional id for it. :) – kkakkurt May 26 '16 at 07:12