控制器始终具有 ModelState.IsValid = False

问题描述 投票:0回答:1

产品型号:

public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }

    public int CategoryId { get; set; }
    [ForeignKey("CategoryId")]
    public Category Category { get; set; }

    public string Description { get; set; }

    public decimal Price { get; set; }
}

类别型号:

public class Category
{
    public int Id { get; set; }
    public string Name { get; set; }
    
    public string Description { get; set; }
}

ProductController 与 ModelState.Isvalid = False

error message

我尝试更改代码并尝试不同的外键方式。

我知道这是一个基本问题,但我无法得到结果。不将产品信息存储到数据库中。

c# asp.net entity-framework
1个回答
0
投票

错误纯粹是因为验证,在 Category 之前使用 [ValidateNever],这样属性永远不会被验证。

© www.soinside.com 2019 - 2024. All rights reserved.