I'm using ASP.NET MVC and jQuery to save some data via AJAX calls. I currently pass in some JSON data using the jQuery ajax() function like so
$.ajax({
dataType: 'json',
type: 'POST',
url: '@Url.Action("UpdateName", "Edit")',
data: {
id: 16,
name: 'Johnny C. Bad'
}
});
using this controller method and helper class.
public void UpdateName(Poco poco)
{
var person = PersonController.GetPerson(poco.Id);
person.Name = poco.Name;
PersonController.UpdatePerson(person);
}
public class Poco
{
public int Id { get; set; }
public string Name { get; set; }
}
Another way of accepting the JSON data is to simply use multiple arguments like this
public void UpdateName(int id, string name)
{
var person = PersonController.GetPerson(id);
person.Name = name;
PersonController.UpdatePerson(person);
}
This approach is ok for a smaller number of arguments, but my real world code has usually has about 5 - 10 arguments. Using an object instead of having to declare and use all these arguments is very handy.
I'm wondernig if there is another way of accepting the JSON data as a single object and not having to declare a class for each controller method I want to use this approach in. For example, something like this:
public void UpdateName(dynamic someData)
{
var person = PersonController.GetPerson(someData.Id);
person.Name = someData.Name;
PersonController.UpdatePerson(person);
}