2017-03-24 56 views
0

我使用的是从剑道UI网格部件上我的Web API应用程序。我的记录被正确加载,甚至删除工作,但不幸的是,邮政更新不工作。我收到了请求响应的错误消息:message:“发生了错误”,exceptionMessage:“值不能为空......”ASP网页API剑道UI网格值不能为空:实体

我的视图和控制器的波纹部分。我错过了什么?尝试很多东西。 *字段名称是小写字母,因为我使用的是CamelCase。

$(document).ready(function() { 
    var dataSource = new kendo.data.DataSource({ 
     transport: { 
      read: { 
       url: "/api/products", 
       dataType: "json" 
      }, 
      update: { 
       url: function (data) { 
        return "/api/products/" + data.id; 
       }, 
       dataType: "json", 
       type: "PUT" 
      }, 
      destroy: { 
       url: function (data) { 
        return "/api/products/" + data.id; 
       }, 
       dataType: "json", 
       type: "DELETE" 
      }, 
      create: { 
       url: "/api/products", 
       dataType: "json", 
       type: "POST" 
      }, 
      parameterMap: function (options, operation) { 
       if (operation !== "read" && options.models) { 
        return { models: kendo.stringify(options.models) }; 
       } 
      } 
     }, 
     batch: false, 
     pageSize: 20, 
     schema: { 
      model: { 
       id: "id", 
       fields: { 
        id: { editable: false, nullable: true }, 
        name: { validation: { required: true } }, 
        description: { validation: { required: true } } 
       } 
      } 
     } 
    }); 

    $("#grid").kendoGrid({ 
     dataSource: dataSource, 
     pageable: true, 
     height: 550, 
     toolbar: ["create"], 
     columns: [ 
      { field: "name", title: "Name" }, 
      { field: "description", title: "Description" }, 
      { command: ["edit", "destroy"], title: " ", width: "250px" 
     }], 
     editable: "popup" 
    }); 

这是我的控制器的一部分,仓库:

public IHttpActionResult GetProducts() 
{ 
    var products = _productRepository.GetProducts(); 
    return Ok(products); 
} 

[HttpPost] 
public IHttpActionResult CreateProduct(Product product) 
{ 
    _productRepository.CreateProduct(product); 
    _productRepository.SaveProduct(); 

    return Ok(); 
} 

[HttpPut] 
public IHttpActionResult UpdateProduct(int id, Product product) 
{ 
    var productInDb = _productRepository.GetProduct(id); 

    if (productInDb == null) 
     return NotFound(); 

    _productRepository.UpdateProduct(product); 
    _productRepository.SaveProduct(); 

    return Ok(); 
} 


public Product GetProduct(int id) 
{ 
    return _context.Products.SingleOrDefault(p => p.Id == id); 
} 

public void CreateProduct(Product product) 
{ 
    _context.Products.Add(product); 
} 

public void UpdateProduct(Product product) 
{ 
    _context.Entry(product).State = EntityState.Modified; 
} 

public void SaveProduct() 
{ 
    _context.SaveChanges(); 
} 

UPDATE 我觉得parameterMap的功能是错摆在首位发布或更新的时候,因为它从来没有在条件进入,所以在这里它更新了参数映射。 POST发生了变化(这不仅仅是关闭窗口并重新填充网格,我不知道为什么)。 但不幸的是更新仍然没有工作,因为我收到的内部控制器以下错误“安装类型‘Models.Product’的实体失败,因为同类型的另一实体已经有相同的主键值。”。在这种情况下我错过了什么?

parameterMap: function (options) { 
    return kendo.stringify(options); 
}, 
type: "json" 
+0

我没有看到***更新***和*** ***消灭在*** ***运输。另外,我在Web API控制器中看不到*** HttpPut ***和*** HttpDelete ***。我错过了什么吗? – Win

+0

对不起,我只是把创建和httppost,但我有他们在我的代码。我认为创建和httppost就足够了。如果需要,我会用其余的代码编辑我的问题。 – jtron

+0

你说***更新***不工作,但你告诉我们***创建***代码。请重新填写问题并用适当的代码更新。 – Win

回答

0

我设法与注释的帮助(基本上是主要问题是parameterMap的,然后是需要填充tweeks以及回传实体的控制方法的返回)来解决它。我在下面放置更新的代码(仅更改了部分)。

查看:

$(document).ready(function() { 
    var dataSource = new kendo.data.DataSource({ 
     transport: { 
      read: { 
       url: "/api/products", 
       dataType: "json" 
      }, 
      update: { 
       url: function (data) { 
        return "/api/products/" + data.id; 
       }, 
       dataType: "json", 
       type: "PUT" 
      }, 
      destroy: { 
       url: function (data) { 
        return "/api/products/" + data.id; 
       }, 
       dataType: "json", 
       type: "DELETE" 
      }, 
      create: { 
       url: "/api/products", 
       dataType: "json", 
       type: "POST" 
      }, 
      parameterMap: function (options) { 
       return kendo.stringify(options); 
      }, 
      type: "json" 
     }, 
     batch: false, 
     pageSize: 20, 
     schema: { 
      model: { 
       id: "id", 
       fields: { 
        id: { editable: false, nullable: true }, 
        name: { validation: { required: true } }, 
        description: { validation: { required: true } } 
       } 
      } 
     } 
    }); 

    $("#grid").kendoGrid({ 
     dataSource: dataSource, 
     pageable: true, 
     height: 550, 
     toolbar: ["create"], 
     columns: [ 
      { field: "name", title: "Name" }, 
      { field: "description", title: "Description" }, 
      { command: ["edit", "destroy"], title: " ", width: "250px" 
     }], 
     editable: "popup" 
    }); 

控制器:

public IHttpActionResult GetProducts() 
{ 
    var products = _productRepository.GetProducts(); 
    return Ok(products); 
} 

[HttpPost] 
public IHttpActionResult CreateProduct(Product product) 
{ 
    _productRepository.CreateProduct(product); 
    _productRepository.SaveProduct(); 

    return Ok(product); 
} 

[HttpPut] 
public IHttpActionResult UpdateProduct(int id, Product product) 
{ 
    _productRepository.UpdateProduct(product); 
    _productRepository.SaveProduct(); 

    return Ok(product); 
}