[英]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) {
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.