ModelState.IsValid在dotnet6控制器中始终为false。

huangapple go评论76阅读模式
英文:

ModelState.IsValid is false always in dotnet6 controller

问题

我的模型是使用数据库优先方法创建的(下面是命令)。

```csharp
Scaffold-DbContext Server=127.0.0.1; database=EmployeePortal; user id = sa; password = <MyStringLocalPassword>;Encrypt=False; Microsoft.EntityFrameworkCore.SqlServer ContextDir Data -OutputDir Models

这里是我的模型:

public partial class Employee
{
    public int Id { get; set; }

    public string Name { get; set; } = null!;

    public string JobDescription { get; set; } = null!;

    public string Number { get; set; } = null!;

    public string Department { get; set; } = null!;

    public decimal HourlyPay { get; set; }

    public decimal Bonus { get; set; }

    public int EmployeeTypeId { get; set; }

    public virtual EmployeeType IdNavigation { get; set; } = null!;
}

public partial class EmployeeType
{
    public int Id { get; set; }

    public string EmployeeType1 { get; set; } = null!;

    public virtual Employee? Employee { get; set; }
}

以下是生成的控制器方法:

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("Id,Name,JobDescription,Number,Department,HourlyPay,Bonus,EmployeeTypeId")] Employee employee)
{
    if (ModelState.IsValid)
    {
    }
}

在这里,对我来说,ModelState.IsValid 总是为 false。

当我检查时,我可以看到以下值。

ModelState.IsValid在dotnet6控制器中始终为false。

我该如何解决这个问题?

我找到了一个堆栈溢出的帖子,其中解决方案是删除并重新创建数据库。

还有其他方法吗?


<details>
<summary>英文:</summary>

My models are created using DB first approach(below command).

    Scaffold-DbContext “Server=127.0.0.1; database=EmployeePortal; user id = sa; password = &lt;MyStringLocalPassword&gt;;Encrypt=False;” Microsoft.EntityFrameworkCore.SqlServer –ContextDir Data -OutputDir Models

Here are my models

    public partial class Employee
    {
    public int Id { get; set; }

    public string Name { get; set; } = null!;

    public string JobDescription { get; set; } = null!;

    public string Number { get; set; } = null!;

    public string Department { get; set; } = null!;

    public decimal HourlyPay { get; set; }

    public decimal Bonus { get; set; }

    public int EmployeeTypeId { get; set; }

    public virtual EmployeeType IdNavigation { get; set; } = null!;
    }

    public partial class EmployeeType
    {
    public int Id { get; set; }

    public string EmployeeType1 { get; set; } = null!;

    public virtual Employee? Employee { get; set; }
    }

Below are the scafolded controller method.

    [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task&lt;IActionResult&gt; Create([Bind(&quot;Id,Name,JobDescription,Number,Department,HourlyPay,Bonus,EmployeeTypeId&quot;)] Employee employee)
        {
            if (ModelState.IsValid)
            {
            }
        }


here for me ModelSatate.IsValid is always false.

I can see the below values when I inspect.

[![enter image description here][1]][1]


  [1]: https://i.stack.imgur.com/ay3Hj.png


How can I resolve this?

I got a stack overflow post where the solution is to delete and recreate the database. 

Is there any other methods?

</details>


# 答案1
**得分**: 0

要实现您的需求,一种方法是您可以从项目文件中删除以下内容:

```xml
<Nullable>enable</Nullable>

(双击项目名称或右键单击项目选择编辑项目文件)。

或者添加?,如下所示:

public virtual EmployeeType? IdNavigation { get; set; } = null!;
英文:

To achieve your requirement, a way is you can remove

&lt;Nullable&gt;enable&lt;/Nullable&gt; 

from your project file
(double-click the project name or right-click the project to choose Edit Project File).

Or add? like:

public virtual EmployeeType? IdNavigation { get; set; } = null!;

huangapple
  • 本文由 发表于 2023年7月10日 18:27:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76652859.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定