2

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)?

ArunPratap
  • 4,816
  • 7
  • 25
  • 43
Jeeten Parmar
  • 5,568
  • 15
  • 62
  • 111

1 Answers1

3

yes you need to extract the model from the form data first e.g.

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

Shaheryar.Akram
  • 722
  • 10
  • 19
Usama Kiyani
  • 195
  • 10