C# 剑道UI添加新记录
我已经使用WebAPI2.2创建了ODataV4服务,我已经成功地将服务记录绑定到网格,但是我无法添加记录。请注意,我为ODataV4服务创建了一个单独的项目,KendoUIGrid在另一个项目中。下面是网格的代码C# 剑道UI添加新记录,c#,c#-4.0,kendo-ui,kendo-grid,C#,C# 4.0,Kendo Ui,Kendo Grid,我已经使用WebAPI2.2创建了ODataV4服务,我已经成功地将服务记录绑定到网格,但是我无法添加记录。请注意,我为ODataV4服务创建了一个单独的项目,KendoUIGrid在另一个项目中。下面是网格的代码 <script> $(document).ready(function () { $("#searchResults").kendoGrid({
<script>
$(document).ready(function () {
$("#searchResults").kendoGrid({
dataSource: {
type: "odata-v4",
transport: {
read:
"http://test.odata.northwind/odata/Customers",
create: {
url: "http://test.odata.northwind/odata/Customers",
dataType: "jsonp", // "jsonp" is required for cross-domain requests; use "json" for same-domain requests
type:"post"
},
parameterMap: function (data, type) {
if (type == "create") {
// send the created data items as the "models" service parameter encoded in JSON
return { models: kendo.stringify(data.models) };
}
}
},
pageSize: 20,
schema: {
data: "value",
model: {
id: "CustomerID",/*
total: function (data) { return data['@@odata.count']; }*/
fields: {
CustomerID: { type: "string" },
CompanyName: { type: "string" },
ContactName: { type: "string" },
ContactTitle: { type: "string" },
Country: { type: "string" }
}
}
}
},
columns: [{
field: "CustomerID",
title: "CustomerID",
filterable: {
cell: {
showOperators: false
}
}
},
{
field: "ContactName",
title: "Contact Name",
filterable: {
cell: {
operator: "contains"
}
},
editor: NameAutoComplete
}, {
field: "ContactTitle",
title: "Contact Title",
filterable: {
cell: {
operator: "contains"
}
},
editor: ContactTitleComboBox
}, {
field: "CompanyName",
title: "Company Name",
filterable: {
cell: {
operator: "contains"
}
}
},
{
field: "Country",
title: "Country",
filterable: {
cell: {
operator: "contains"
}
}
, editor: categoryDropDownEditor
},
{ command: ["edit", "destroy"], title: " ", width: "250px" }
],
height: 550,
toolbar: ["create", "excel", "pdf"],
excel: {
fileName: "Kendo UI Grid Export.xlsx",
proxyURL: "http://demos.telerik.com/kendo-ui/service/export",
filterable: true
}, pdf: {
allPages: true,
fileName: "Kendo UI Grid Export.pdf",
proxyURL: "http://demos.telerik.com/kendo-ui/service/export"
},
scrollable: false,
pageable: true,
sortable: true,
groupable: true,
filterable: {
mode: "row"
},
editable: {
mode: "inline",
create: true,
update: true,
destroy: true
}
});
});
function categoryDropDownEditor(container, options) {
$('<input required data-text-field="Country" data-value-field="CustomerID" data-bind="value:' + options.field + '"/>')
.appendTo(container)
.width(100)
.kendoDropDownList({
autoBind: false,
dataSource: {
type: "odata-v4",
transport: {
read: "http://test.odata.northwind/odata/Customers"
}
}
});
}
function NameAutoComplete(container, options) {
$('<input data-text-field="ContactName" data-value-field="CustomerID" data-bind="value:' + options.field + '"/>')
.appendTo(container)
.kendoAutoComplete({
autoBind: false,
dataSource: {
type: "odata-v4",
transport: {
read: "http://test.odata.northwind/odata/Customers"
}
}
});
}
function ContactTitleComboBox(container, options) {
$('<input data-text-field="ContactTitle" data-value-field="CustomerID" data-bind="value:' + options.field + '"/>')
.appendTo(container)
.kendoComboBox({
autoBind: false,
dataSource: {
type: "odata-v4",
transport: {
read: "http://test.odata.northwind/odata/Customers"
}
}
});
}
</script>
$(文档).ready(函数(){
$(“#搜索结果”).kendoGrid({
数据源:{
类型:“odata-v4”,
运输:{
阅读:
"http://test.odata.northwind/odata/Customers",
创建:{
url:“http://test.odata.northwind/odata/Customers",
数据类型:跨域请求需要“jsonp”//“jsonp”;相同域请求需要使用“json”
类型:“职位”
},
parameterMap:函数(数据,类型){
如果(类型==“创建”){
//将创建的数据项作为JSON编码的“models”服务参数发送
返回{models:kendo.stringify(data.models)};
}
}
},
页面大小:20,
模式:{
数据:“价值”,
型号:{
id:“CustomerID”/*
总计:函数(数据){返回数据['@@odata.count'];}*/
字段:{
CustomerID:{type:“string”},
公司名称:{type:“string”},
联系人姓名:{type:“string”},
ContactTitle:{type:“string”},
国家:{type:“string”}
}
}
}
},
栏目:[{
字段:“CustomerID”,
标题:“CustomerID”,
可过滤:{
单元格:{
showOperators:false
}
}
},
{
字段:“联系人姓名”,
标题:“联系人姓名”,
可过滤:{
单元格:{
操作员:“包含”
}
},
编辑:NameAutoComplete
}, {
字段:“ContactTitle”,
标题:“联系人标题”,
可过滤:{
单元格:{
操作员:“包含”
}
},
编辑:ContactTitleComboBox
}, {
字段:“公司名称”,
标题:“公司名称”,
可过滤:{
单元格:{
操作员:“包含”
}
}
},
{
字段:“国家”,
标题:“国家”,
可过滤:{
单元格:{
操作员:“包含”
}
}
,编者:CategorHydropdowneditor
},
{命令:[“编辑”、“销毁”],标题:,宽度:“250px”}
],
身高:550,
工具栏:[“创建”、“excel”、“pdf”],
卓越:{
文件名:“剑道UI网格导出.xlsx”,
代理URL:“http://demos.telerik.com/kendo-ui/service/export",
可过滤:真
},pdf:{
是的,
文件名:“剑道UI网格导出.pdf”,
代理URL:“http://demos.telerik.com/kendo-ui/service/export"
},
可滚动:false,
pageable:对,
可排序:是的,
分组:对,
可过滤:{
模式:“行”
},
可编辑:{
模式:“内联”,
创造:真的,
更新:对,,
毁灭:真的
}
});
});
函数类别HydropDownEditor(容器、选项){
$('')
.appendTo(容器)
.宽度(100)
.kendoDropDownList({
自动绑定:错误,
数据源:{
类型:“odata-v4”,
运输:{
阅读:“http://test.odata.northwind/odata
public class CustomersController : ODataController
{
readonly Model1 _db = new Model1();
[EnableQuery(PageSize = 20)]
public IHttpActionResult Get()
{
return Ok(_db.Customers.AsQueryable());
}
public IHttpActionResult Get([FromODataUri] string key)
{
return Ok(_db.Customers.SingleOrDefault(t => t.CustomerID == key));
}
[System.Web.Mvc.HttpPost]
public async Task<IHttpActionResult> Post(Customers customer)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
_db.Customers.Add(customer);
await _db.SaveChangesAsync();
return Created(customer);
}
[System.Web.Mvc.HttpDelete]
public async Task<IHttpActionResult> Delete([FromODataUri] int key)
{
var customers = await _db.Customers.FindAsync(key);
if (customers == null)
{
return NotFound();
}
_db.Customers.Remove(customers);
await _db.SaveChangesAsync();
return StatusCode(HttpStatusCode.NoContent);
}
protected override void Dispose(bool disposing)
{
_db.Dispose();
base.Dispose(disposing);
}
}
editable: {
mode: "inline",
create: true,
update: true,
destroy: true
}