简体   繁体   English

根据Model Asp.net Core API验证Json String

[英]Validate Json String against Model asp.net core api

I need to send Model data along with Image so I am using FormData . 我需要将Model数据与Image一起发送,因此我正在使用FormData As we can't pass model directly, I am using JSON.stringify . 由于我们无法直接传递模型,因此我正在使用JSON.stringify

How do I validate this Json string against Model (Same as we do ModelState validation)? 如何针对Model验证此Json字符串(与进行ModelState验证相同)?

yes you need to extract the model from the form data first eg 是的,您需要首先从表单数据中提取模型,例如

var request = HttpContext.Current.Request;
var model = new yourViewModel();
model.field1 = request.Form["field1"];
model.field2 = request.Form["field2"];
model.Document = request.Files["Document"];

ModelState.Clear(); 
this.Validate(model); 
if (ModelState.IsValid) {

}

Read more here 在这里阅读更多

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

相关问题 在 Asp.net Core 中使用 Json 发送模型 - Send Model With Json In Asp.net Core 如何在ASP.NET Core 2 Web API中仅验证模型状态的一部分 - How to validate only a part of Model State in asp.net core 2 web api 如何验证模型取决于使用ASP.NET Core Web API的布尔数据类型值 - How to validate model depends on bool datatype value using asp.net core web api 如何有条件地验证 ASP.NET 核心 Web ZDB974238714CA8DE634A7CE1D08 中的整个 model? - How to conditionally validate entire model in ASP.NET Core Web API? asp.net core api 模型中的 datetimeoffset - datetimeoffset in the asp.net core api model 如何通过仅允许在 asp.net 核心 web Z8A35DA52ED1057271 的 Model 中分配的属性来验证请求正文 - How to Validate Request Body by allowing only attributes assigned in the Model of an asp.net core web api app ASP.NET Core API 模型概念 - ASP.NET Core API model concept 如何在 ASP.NET 核心中使用 model 验证来验证所需的查询字符串字段 - How to validate a required query string field using model validation in ASP.NET Core 验证ASP.NET Core API中格式无效的值 - Validate values with invalid format in ASP.NET Core API ASP.NET Core Web API - Get file along with [FromBody] JSON model - ASP.NET Core Web API - Get file along with [FromBody] JSON model
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM