ASP Web API - Kendo 网格和模型状态

ASP Web API - Kendo Grid and Model State

我在 Web API post 方法上使用 Kendo 网格和 ModelState.IsValid 条件。当我在网格上创建新记录时(实际上我正在使用网格的弹出选项来创建新记录),它将我的 class 的 ID 发送为 null,然后当它到达我的控制器时ModelState 总是无效的,因为它期望我的 class 的 Id 为 0。当操作为 'create' 时,我通过更改数据源的 parameterMap 上的 Id 值来解决它(见下面的代码),但我真的不知道这是否是最好的解决方案,因为在我看来这是一种糟糕的方式。还有另一种选择可以解决这个问题吗?谢谢。

查看:

$(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"
    });
});

控制器(我只放了 post 方法,因为它是出现问题的方法):

[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();
    }

可能 id 字段是 nullable: true

能否将其删除并添加 type: "number"

fields: {
   id: { editable: false, type: "number" },
   name: { validation: { required: true } },
   description: { validation: { required: true } }
}