如何防止 .NET Core Razor 页面中只读表单上的所需验证
我有一个只读表单,它的唯一功能是搜索提供的参数并使用结果填充表单,我将其绑定到模型,但模型使用所需的数据注释,当我单击搜索按钮时,ModelState .IsValid 是 false,当我检查它所抱怨的属性时,我发现它甚至不是我在表单上使用的属性,因为我也使用此模型进行插入和更新,并且当我使用它时它具有所需的属性在只读场景中,表格返回无效。如何停止对模型中未在表单中使用或应该只读的必需成员进行验证?
I have a read only form where it's only function is to search on the provided parameter and populate the form with the results, I am binding it to a model but the model uses the Required data annotation and when I click the search button, the ModelState.IsValid is false, when I checked to see which property it's complaining about, I found it's not even one I'm using on the form so because I also use this model for insert and updates and it has required properties, when I use it in a read only scenario, the form comes back invalid. How can I stop the validation from occurring on the Required members in my model which aren't used on my form or supposed to be read only?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果表单用于编辑已搜索的记录,那么我怀疑您的模型注释适用。
如果表单不是用于编辑的,那么它就不应该是表单。只是字段的集合(仍然可以绑定到模型)。这样,由 EditForm 管理的自动验证就不会发生。
或者,如果您发现您没有使用模型的所有属性,那么您应该考虑创建一个专门用于在此视图面板中显示数据的视图模型(而不是编辑模型)。
If the form is for editing the record that has been searched, then I'd suspect that your model annotations apply.
If the form is not for editing, then it shouldn't be a form. Just a collection of fields (that can still be bound to the model). In this way the automated validations managed by the EditForm will not happen.
Or, if as you've discovered you're not using all of the properties of the model, then you should consider creating a view model (not an edit model) specific for the display of data in this view panel.