0

I have an HTML table. This table has a contenteditable td, so when the user clicks a button, the text on that editable td gets passed to my POST action on the controller.

I think is simple, but I don't know how to do it, and all the related answers that I found include JavaScript code. I think is unnecessary to this case. How can I do this with using form method in the view code?

This is my code on the view

<table class="table table-hover" style="white-space:nowrap;">
    <tr>
        <th>Campo</th>
        <th>Columna en Excel</th>
    </tr>
    <tr>
        <td> Nombre</td>
        <td contenteditable="true" title="editar"  > Nombre en excel</td>
    </tr>
</table>

Note that I'm using ASP.NET MVC Framework on the server side.

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
  • The reason this requires JavaScript code is because the content of `td`s doesn't get submitted to the server. With a standard form submission, only form elements (such as `input`) which are within the `form` element, and contain a `name` attribute will get submitted. When you use JavaScript, you're typically [copying the value of the `td` to e.g. a `hidden` form element](https://stackoverflow.com/a/6247785/3025856), and then submitting that—or, alternatively, submitting the value to the server directly using AJAX. – Jeremy Caney Aug 14 '20 at 21:22
  • One alternative that _might_ work for you is to put e.g. a `textarea` element inside of your `td` and then use CSS styles to make it look like a standard text block when it's not active, and then look like an input field when the user clicks on it. That would give you something comparable to `contenteditable`, while also ensuring that your data gets submitted with the form. – Jeremy Caney Aug 14 '20 at 21:24
  • thanks everybody for the answers, i finally understand why i have to use javascript or add a text area inside the td. – HOLDTHEDOOR Aug 15 '20 at 00:58

1 Answers1

0

You will have to use a textarea, there is no way you will find that submits anything to the server that is not part of a form. If you want to use something other than standard form elements you will need to use JavaScript. This is how forms work.

Adrian Brand
  • 20,384
  • 4
  • 39
  • 60