The AppendRow method is not being executed. I am assuming it's somewhere in the AJAX call below. There is no error, it just goes to sleep. Can anyone see what I've missed? Thanks.
+++++++++++++++++++++++++++++++++ Default.aspx +++++++++++++++++++++++++++
$(document).ready(function () {
getProducts();
$("#btn").on('click', function () {
//1.
//var order = {
// "CustomerName": "MS"
//};
//2.
var prodDTO = [
{ "Id": "4", "Name": "Guava Mango Drink", "Category": "Beverages", "Price": "1.50" },
];
//3.
$.ajax({
url: '/api/products/AppendRow',
type: 'POST',
//4.
data: {
//order: order,
prodDTO: prodDTO
},
ContentType: 'application/json;utf-8',
datatype: 'json'
}).done(function (resp) {
alert("Successful " + resp);
}).error(function (err) {
alert("Error " + err.status);
});
});
return false;
});
+++++++++ Product.cs +++++++++++++++++++++++
using Newtonsoft.Json;
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public string Category { get; set; }
}
//public class ProductDTO
//{
//public int Id { get; set; }
//public string Name { get; set; }
//public string Category { get; set; }
//public double Price { get; set; }
//}
public class YourObjectDto
{
[JsonProperty("id")]
public int Id { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("category")]
public string Category { get; set; }
[JsonProperty("price")]
public double Price { get; set; }
}
++++++++++++++++++++++++++++++++++++++ ProductsController.cs +++++++++++++++++++++++++++++
namespace WebForms
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
public class ProductsController : ApiController
{
Product[] products = new Product[]
{
new Product { Id = 1, Name = "Tomato Soup", Category = "Groceries", Price = 1 },
new Product { Id = 2, Name = "Yo-yo", Category = "Toys", Price = 3.75M },
new Product { Id = 3, Name = "Hammer", Category = "Hardware", Price = 16.99M }
};
public IEnumerable<Product> GetAllProducts()
{
return products;
}
public Product GetProductById(int id)
{
var product = products.FirstOrDefault((p) => p.Id == id);
if (product == null)
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
return product;
}
public IEnumerable<Product> GetProductsByCategory(string category)
{
return products.Where(
(p) => string.Equals(p.Category, category,
StringComparison.OrdinalIgnoreCase));
}
public string AppendRow([FromBody] YourObjectDto eventDto)
{
var id = eventDto.Id;
var name = eventDto.Name;
var category = eventDto.Category;
var price = eventDto.Price;
return "success";
}
}
}