Yes, this is possible. The model binder can examine the posted values and attempt to convert them to your model object.
From the documentation
A model binder in MVC provides a simple way to map posted form values to a .NET Framework type and pass the type to an action method as a parameter. Binders also give you control over the deserialization of types that are passed to action methods. Model binders are like type converters, because they can convert HTTP requests into objects that are passed to an action method. However, they also have information about the current controller context.
As far as your current code, no changes need to be made to the server other than converting your method to return an ActionResult and making sure it's in a controller, and has a route.
public ActionResult saveData(MyClass postData)
{
Database.SaveData(postData); //or however you save to your database
return RedirectToAction("Success");
}
Your form on the client should of course have an action
that points to a URL that is mapped to your saveData
action method's route. And for posting a simple object, the name attribute of the form elements should match the property names on the model object.
<form action="MyController/saveData">
<input type="text" id="a" value="aaaa" name="a">
<input type="text" id="b" value="bbbb" name="b">
</form>
Though using the MVC helper for BeginForm
as in Alex's answer is also possible.
And your model object should have public properties so the model binder can set them:
public class MyClass
{
public string a {get; set;}
public string b {get; set; }
}