0

As the title says, how can I submit values inside input fields that reside in a table? My table is loaded with razor foreach like:

@foreach (var i in Model)
{
    <tr>
        <td>@i.Id</td>
        <td><input type="text" value="@i.Name" name="Name" /></td>
        <td><input type="text" value="@i.Weight" name="Weight" /></td>
        <td>
            @Html.DropDownListFor(m => m.Gender, Model.DropdownGenders)
        </td>
    </tr>
}
....
<button type="button">Save Changes</button>

There could be unknown number of rows but what I want to do is when I click the Save Changes button, each row will be transformed into a json string probably like this:

[
  {id: 1, name: "name", weight: "weight", gender: "gender"},
  {id: 2, name: "name", weight: "weight", gender: "gender"}
  ..
]

So that I can submit it as a string for deserialization in my MVC controller. Im trying to avoid using any 3rd party libraries except jquery. Help please.

super-user
  • 1,017
  • 4
  • 17
  • 41
  • You can create the json like in question right? – mr. Sep 05 '16 at 09:59
  • Refer [this answer](http://stackoverflow.com/questions/39265053/passing-json-into-mvc-controller/39326423#39326423), but you would be far better off generating your view correctly using a `for` loop or custom `EditorTemplate` (refer [this answer](http://stackoverflow.com/questions/30094047/html-table-to-ado-net-datatable/30094943#30094943)) in which case all you need is `$.post(yourUrl, $('form').serialize(), function(data) { // do something with the data you return });` –  Sep 05 '16 at 09:59

0 Answers0