views:

39

answers:

2

Can you bind to an object when posting with jQuery?

You can call this controller action

 public ActionResult AddCar(Car myCar)
 {
   . . .
 }

Where the properties of the form using the binding of ASP.NET MVC to populate the properties of the car.

If I am posting via ajax and jQuery can I do the same thing?

A: 

Yes, you can.
You just need to make sure the keys in the form correspond to the Car object's properties.

çağdaş
+1  A: 

If you have an HTML form containing inputs to bind to the object:

$.ajax({
    url: '/AddCar',
    data: $('#yourFormId').serialize(),
    success: function(data) {
        alert('success');
    }
});

Or if you don't have a form and you want to bind the object properties manually:

$.ajax({
    url: '/AddCar',
    data: { make: 'Peugeot', model: '407', year: '2009' },
    success: function(data) {
        alert('success');
    }
});
Darin Dimitrov