英文:
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。
当我检查时,我可以看到以下值。
我该如何解决这个问题?
我找到了一个堆栈溢出的帖子,其中解决方案是删除并重新创建数据库。
还有其他方法吗?
<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 = <MyStringLocalPassword>;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<IActionResult> Create([Bind("Id,Name,JobDescription,Number,Department,HourlyPay,Bonus,EmployeeTypeId")] 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
<Nullable>enable</Nullable>
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!;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论