【问题标题】:ASP Web API - Kendo Grid and Model StateASP Web API - 剑道网格和模型状态
【发布时间】:2017-08-20 10:13:18
【问题描述】:

我在我的 Web API 发布方法上使用了 Kendo Grid 和 ModelState.IsValid 条件。当我在网格上创建新记录时(实际上我正在使用网格的弹出选项来创建新记录),它将我的类的 Id 发送为 null,然后当涉及到我的控制器时,ModelState 总是无效,因为它希望我的类的 Id 为 0。当操作为“创建”时,我通过更改数据源的 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();
    }

【问题讨论】:

  • 你能显示视图(其余代码)和产品模型吗?
  • 我用完整的代码更新了这个问题。谢谢。

标签: asp.net asp.net-web-api kendo-ui kendo-grid kendo-asp.net-mvc


【解决方案1】:

id 字段可能是nullable: true

您可以删除它并添加type: "number"吗?

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-21
    • 1970-01-01
    • 2014-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多