Hi,
Apologies in advance - just trying to get started with this, and to compound things, I'm new to WebAPI too.
I've taken the inline grid demo, and made a few adjustments. I also used the MVC5 WebAPI template to create a new JSON service I could call, but can't seem to get it all working together.
The grid initializes properly, and does a GET to return and show the three rows. However, when I attempt to update/create/delete, it always calls the POST method on the server, and the value passed is always unititialised. Can someone please advise where I'm going wrong?
My model looks like this:
namespace WebAPI2.Models
{
public class Product
{
public int ProductID { get; set; }
public string ProductName {get;set;}
public int UnitPrice { get; set; }
public bool Discontinued { get; set; }
public int UnitsInStock { get; set; }
}
}
The controller:
namespace WebAPI2.Controllers
{
public class ProductsController : ApiController
{
Product[] products = new Product[]
{
new Product { ProductID = 1, ProductName = "Prod_1", UnitPrice = 1, Discontinued=false, UnitsInStock=1 },
new Product { ProductID = 2, ProductName = "Prod_2", UnitPrice = 2, Discontinued=true, UnitsInStock=2 },
new Product { ProductID = 3, ProductName = "Prod_3", UnitPrice = 3, Discontinued=false, UnitsInStock=3 },
};
// GET api/products
public IEnumerable<Product> Get()
{
return products;
}
// GET api/products/5
public Product Get(int id)
{
return products[id];
}
// POST api/products
public void Post([FromBody]Product value)
{
//just for a breakpoint
int i = 1;
}
// PUT api/products/5
public void Put(int id, [FromBody]Product value)
{
//just for a breakpoint
int i = 1;
}
// DELETE api/products/5
public void Delete(int id)
{
//just for a breakpoint
int i = 1;
}
}
}
And finally the client:
<div id="example">
<div id="grid"></div>
<script>
$(document).ready(function () {
var crudServiceBaseUrl = "http://demos.telerik.com/kendo-ui/service",
dataSource = new kendo.data.DataSource({
transport: {
read: {
//url: crudServiceBaseUrl + "/Products",
//dataType: "jsonp"
url: "http://localhost:63272/api/products",
dataType: "json",
type: "GET"
},
update: {
//url: crudServiceBaseUrl + "/Products/Update",
//dataType: "jsonp"
url: function (product) {
return "http://localhost:63272/api/products/" + product.ProductID;
},
dataType: "json",
type: "PUT"
},
destroy: {
//url: crudServiceBaseUrl + "/Products/Destroy",
//dataType: "jsonp"
url: function (product) {
return "http://localhost:63272/api/products/" + product.ProductID;
},
dataType: "json",
type: "DELETE"
},
create: {
//url: crudServiceBaseUrl + "/Products/Create",
//dataType: "jsonp"
url: "http://localhost:63272/api/products",
dataType: "json",
type: "POST"
},
parameterMap: function (options, operation) {
if (operation !== "read" && options.models) {
return { models: kendo.stringify(options.models) };
}
}
},
batch: true,
pageSize: 20,
schema: {
model: {
//id: "ProductID",
fields: {
ProductID: { editable: false, nullable: true },
ProductName: { validation: { required: true } },
UnitPrice: { type: "number", validation: { required: true, min: 1 } },
Discontinued: { type: "boolean" },
UnitsInStock: { type: "number", validation: { min: 0, required: true } }
}
}
}
});
$("#grid").kendoGrid({
dataSource: dataSource,
pageable: true,
height: 550,
toolbar: ["create"],
columns: [
"ProductName",
{ field: "UnitPrice", title: "Unit Price", format: "{0:c}", width: "120px" },
{ field: "UnitsInStock", title: "Units In Stock", width: "120px" },
{ field: "Discontinued", width: "120px" },
{ command: ["edit", "destroy"], title: " ", width: "200px" }],
editable: "inline"
});
});
</script>
</div>
Thanks in advance for any help!
Apologies in advance - just trying to get started with this, and to compound things, I'm new to WebAPI too.
I've taken the inline grid demo, and made a few adjustments. I also used the MVC5 WebAPI template to create a new JSON service I could call, but can't seem to get it all working together.
The grid initializes properly, and does a GET to return and show the three rows. However, when I attempt to update/create/delete, it always calls the POST method on the server, and the value passed is always unititialised. Can someone please advise where I'm going wrong?
My model looks like this:
namespace WebAPI2.Models
{
public class Product
{
public int ProductID { get; set; }
public string ProductName {get;set;}
public int UnitPrice { get; set; }
public bool Discontinued { get; set; }
public int UnitsInStock { get; set; }
}
}
The controller:
namespace WebAPI2.Controllers
{
public class ProductsController : ApiController
{
Product[] products = new Product[]
{
new Product { ProductID = 1, ProductName = "Prod_1", UnitPrice = 1, Discontinued=false, UnitsInStock=1 },
new Product { ProductID = 2, ProductName = "Prod_2", UnitPrice = 2, Discontinued=true, UnitsInStock=2 },
new Product { ProductID = 3, ProductName = "Prod_3", UnitPrice = 3, Discontinued=false, UnitsInStock=3 },
};
// GET api/products
public IEnumerable<Product> Get()
{
return products;
}
// GET api/products/5
public Product Get(int id)
{
return products[id];
}
// POST api/products
public void Post([FromBody]Product value)
{
//just for a breakpoint
int i = 1;
}
// PUT api/products/5
public void Put(int id, [FromBody]Product value)
{
//just for a breakpoint
int i = 1;
}
// DELETE api/products/5
public void Delete(int id)
{
//just for a breakpoint
int i = 1;
}
}
}
And finally the client:
<div id="example">
<div id="grid"></div>
<script>
$(document).ready(function () {
var crudServiceBaseUrl = "http://demos.telerik.com/kendo-ui/service",
dataSource = new kendo.data.DataSource({
transport: {
read: {
//url: crudServiceBaseUrl + "/Products",
//dataType: "jsonp"
url: "http://localhost:63272/api/products",
dataType: "json",
type: "GET"
},
update: {
//url: crudServiceBaseUrl + "/Products/Update",
//dataType: "jsonp"
url: function (product) {
return "http://localhost:63272/api/products/" + product.ProductID;
},
dataType: "json",
type: "PUT"
},
destroy: {
//url: crudServiceBaseUrl + "/Products/Destroy",
//dataType: "jsonp"
url: function (product) {
return "http://localhost:63272/api/products/" + product.ProductID;
},
dataType: "json",
type: "DELETE"
},
create: {
//url: crudServiceBaseUrl + "/Products/Create",
//dataType: "jsonp"
url: "http://localhost:63272/api/products",
dataType: "json",
type: "POST"
},
parameterMap: function (options, operation) {
if (operation !== "read" && options.models) {
return { models: kendo.stringify(options.models) };
}
}
},
batch: true,
pageSize: 20,
schema: {
model: {
//id: "ProductID",
fields: {
ProductID: { editable: false, nullable: true },
ProductName: { validation: { required: true } },
UnitPrice: { type: "number", validation: { required: true, min: 1 } },
Discontinued: { type: "boolean" },
UnitsInStock: { type: "number", validation: { min: 0, required: true } }
}
}
}
});
$("#grid").kendoGrid({
dataSource: dataSource,
pageable: true,
height: 550,
toolbar: ["create"],
columns: [
"ProductName",
{ field: "UnitPrice", title: "Unit Price", format: "{0:c}", width: "120px" },
{ field: "UnitsInStock", title: "Units In Stock", width: "120px" },
{ field: "Discontinued", width: "120px" },
{ command: ["edit", "destroy"], title: " ", width: "200px" }],
editable: "inline"
});
});
</script>
</div>
Thanks in advance for any help!