I have an ASP.NET MVC 5 application with a Test class in my model and a ModelTest function in my controller.
I would like to be able to use ModelState.IsValid when I call the action using AJAX, so passing a JSON object. I tried this
public JsonResult Modeltest(Test input) {
if (ModelState.IsValid) {
return Json(new { Response = "Success" }, JsonRequestBehavior.AllowGet);
}
return Json(new { Response = "Error" }, JsonRequestBehavior.AllowGet);
}
The Test class is pretty straightforward
public class Test {
public int num { get; set; }
public string name { get; set; }
}
And the call will be something like this http://myServer/myController/ModelTest?input={"num":5, "name ":"myName"}
Of course it doesn't works and if I put a breakpoint at the first line in the server the input object is NULL. Is no only possible solution pass a JSON string, deserializing it and manually fill a model object? Isn't there a faster (and automated) way to parse the input as the specified class?
http://myServer/myController/ModelTest?num=5&name=myNameshow your ajax call aswell