Search code examples
jqueryjsonserializationasp.net-mvc-3

Any quick ways to serialize input data to List<object> with jQuery?


Let's say I have an object:

Person
{
   public string FirstName {get;set;}
   public string LastName {get;set;}
}

On the client side, the user gets a table with a whole bunch of input texts for adding/editing these "people".

<tr>
  <td><input type="text" name="FirstName"/></td>
  <td><input type="text" name="LastName"/></td>
</tr>
<tr>
  <td><input type="text" name="FirstName"/></td>
  <td><input type="text" name="LastName"/></td>
</tr>
<tr>
  <td><input type="text" name="FirstName"/></td>
  <td><input type="text" name="LastName"/></td>
</tr>

The controller action is expecting a List<Person>:

public ActionResult SavePeople (List<Person> people)
{ ..... }

Input data is submitted to the Action with AJAX call. What is the easiest way to serialize all that input data? I don't want to manually build a JavaScript array, etc. Something like $('table input').serialize()...


Solution

  • If it's already wrapped in a form...

    $('form').serialize();