I have inherited a code-base that uses ASP.NET MVC 3. I’m learning it as I go. This project appears to expose an API of the format “/svcs/orders/”. There is a method that is already written in a file located in MyProject.Areas.svcs.Controllers.OrdersController. That controller looks like the following:
public class OrdersController : Controller
{
[AcceptVerbs(HttpVerbs.Put)]
public ActionResult AddOrder(OrderModel data)
{
// Do Stuff...
var result = GetResult();
return Json(result, JsonRequestBehavior.AllowGet);
}
}
I am trying to figure out how to call this method via JQuery. I know that I can reference the service via an address of “/svcs/orders/”. But I’m not sure how to “PUT” data to the service. How do I populate the OrderModel parameter that is expected by the AddOrder method? Its so bizarre to me. Currently, I’m trying the following:
var productID = $("#hidProductID").val();
var quantity = $("#txtQuantity").val();
$.ajax({
url: '/svcs/orders',
type: 'PUT',
contentType: 'application/json; charset=utf-8',
success: function (result) {
alert(result);
},
error: function() {
alert("There was an error.");
}
});
Thank you so much for any help you can provide!
You can pass
OrderModelin the data property of ajax options in the form ofJSONobject.