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)?
1 Answer
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
2 Comments
Jeeten Parmar
Thanks. It worked. I am not sure if I can use
ActionFilterAttribute in this scenario.Usama Kiyani
i guess you can check this [post stackoverflow.com/questions/10673649/…
FormDatafor large images also?