简体   繁体   中英

Validate Json String against Model asp.net core api

I need to send Model data along with Image so I am using FormData . As we can't pass model directly, I am using JSON.stringify .

How do I validate this Json string against Model (Same as we do ModelState validation)?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM