简体   繁体   English

ASP.NET 内核 Razor PageModel ModelState 无效,因为新的导航属性 Id==0

[英]ASP.NET Core Razor PageModel ModelState Invalid because of new Navigation Property with Id==0

I'm trying to update an entity that may have an optional NavigationProperty from an html form.我正在尝试从 html 表单更新可能具有可选 NavigationProperty 的实体。

public class ProcessDataGroup
{
    public int Id { get; set; }
    public string Name { get; set; }
    public ReadDataGroupStrategy ReadDataGroupStrategy { get; set; }
    public ReadBlockDefinition ReadBlockDefinition { get; set; }
    public int? ReadBlockDefinitionId { get; set; }
}

public class ReadBlockDefinition
{
    public int Id { get; set; }
    public string Address { get; set; }
    public int Length { get; set; }
    public int Offset { get; set; }
}

If ReadBlockDefinition already exists and has an Id > 0 , this works.如果ReadBlockDefinition已经存在并且Id > 0 ,则此方法有效。 I attach the entity to my DbContext and it will by updated.我将实体附加到我的DbContext并且它将被更新。 But it is possible that in the html form the ReadDataGroupStrategy is changed and then a new ReadBlockDefinition is created, that by default of course has an Id of 0. In this case ModelState.IsValid will return false und investigation during debugging discovers that is it complaining about the ReadBlockDefinition.Id field, so it seems it won't accept 0 there.但是有可能在 html 表单中更改了ReadDataGroupStrategy ,然后创建了一个新的ReadBlockDefinition ,默认情况下它的Id为 0。在这种情况下, ModelState.IsValid将返回 false,调试期间的调查发现它在抱怨关于ReadBlockDefinition.Id字段,所以它似乎不会接受 0 那里。 I cannot skip the Id field from the ReadBlockDefinition ViewModel because it is essential for updates.我不能跳过ReadBlockDefinition ViewModel 中的 Id 字段,因为它对于更新至关重要。

My workaround at the moment is我目前的解决方法是

if(ProcessDataGroup?.ReadBlockDefinition?.Id == 0)
    ModelState.Remove("ProcessDataGroup.ReadBlockDefinition.Id");

before evaluating ModelState.IsValid but this seems a rather strange solution to me.在评估ModelState.IsValid之前,但这对我来说似乎是一个相当奇怪的解决方案。 I'm generally not sure why validation fails in the first place.我通常不确定为什么验证首先会失败。 As I understood so far, validation isn't related to EF Core or whatever underlying technology and 0 should be a perfectly valid integer.据我所知,验证与 EF Core 或任何底层技术无关,0 应该是完全有效的 integer。

Any ideas how to overcome this?任何想法如何克服这个问题? Or is the ModelState.Remove(...) approach the intended one?还是ModelState.Remove(...)方法是预期的?

Have you considered setting the data annotations for Id?您是否考虑过为 Id 设置数据注释?

public class ReadBlockDefinition
{
    [Key]
    public int Id { get; set; }
    public string Address { get; set; }
    public int Length { get; set; }
    public int Offset { get; set; }
}

https://docs.microsoft.com/en-us/ef/ef6/modeling/code-first/data-annotations https://docs.microsoft.com/en-us/ef/ef6/modeling/code-first/data-annotations

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 简单的注入器注入PageModel ASP.NET核心Razor页面 - Simple Injector Inject into PageModel ASP.NET Core Razor Pages 如何在 PageModel 中为表单分配属性(按按钮),维护选定的页面布局 [ASP.NET/Razor 页面] - How to assign property in PageModel (by button) for form, maintaining selected page layout [ASP.NET/Razor Page] ASP.NET Core 3.0 形式 ModelState 始终无效 - ASP.NET Core 3.0 form ModelState always invalid ASP.NET Razor - 在一个 PageModel 中绑定多个属性和表单 - ASP.NET Razor - Binding multiple properties and forms in one PageModel 有没有什么简单的方法可以将复杂对象从一个 PageModel 文件传递​​到 asp.net 核心剃刀页面(mvvm,而不是 mvc)中的另一个? - Is there any easy way to to pass complex objects from one PageModel file to another in asp.net core razor pages (mvvm, not mvc)? 如何仅为 ASP.Net Core Razor 页面中的一个 Model 启用 ModelState 验证? - How enable ModelState validation just for one Model in ASP.Net Core Razor Pages? ASP.NET MVC 5“ ModelState无效时” - ASP.NET MVC 5 “When ModelState is Invalid” 在 Asp.net 核心中间件中访问 ModelState - Access ModelState in Asp.net core Middleware ASP.NET 核心 Razor 使用属性作为后表单的基础 - ASP.NET Core Razor using property as base for post form 在 ASP.NET Core 中将数据发布到数据库时出现问题,ModelState 无效 - Problem while posting data to the database in ASP.NET Core, ModelState is invalid
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM