웹 웹 게시 메소드에서 Kendo Grid 및 ModelState.IsValid 조건을 사용하고 있습니다. 모눈에 새 레코드를 만들면 (실제로 모눈의 팝업 옵션을 사용하여 새 레코드를 만듭니다), 내 클래스의 ID를 null로 보내고 내 컨트롤러에 대해서는 ModelState가 항상 왜냐하면 그것은 내 클래스의 이드가 0이 될 것으로 예상하기 때문에 무효합니다. 저는 작업이 'create'(아래 코드 참조) 일 때 datasource의 parameterMap에서 ID의 값을 변경하여이 문제를 해결했지만 실제로 그것은 나에게 가난한 길을 좋아하는 것처럼 보이는 가장 좋은 해결책입니다. 이 문제를 해결할 다른 방법이 있습니까? 감사.ASP 웹 API - 검도 그리드 및 모델 상태
보기 :
$(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) {
// THIS IS MY FIX FOR NOW
if (operation === "create") {
options.id = 0;
}
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"
});
});
컨트롤러 (이 문제를 가지고 사람이었다 나는 단지 포스트 방법을 넣어) :
[HttpPost]
public IHttpActionResult CreateProduct(Product product)
{
if (!ModelState.IsValid)
return BadRequest();
_productRepository.CreateProduct(product);
_productRepository.SaveProduct();
return Ok(product);
}
모델 :
public class Product
{
public int Id { get; set; }
[Required]
[StringLength(100)]
public string Name { get; set; }
[Required]
[StringLength(255)]
public string Description { get; set; }
}
저장소 :
public void CreateProduct(Product product)
{
_context.Products.Add(product);
}
public void SaveProduct()
{
_context.SaveChanges();
}
View (* 나머지 코드) * 및 제품 모델을 표시 할 수 있습니까? – Win
전체 코드로 질문을 업데이트했습니다. 감사. – jtron