0

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?

2
  • 1
    You should be doing this instead: http://myServer/myController/ModelTest?num=5&name=myName show your ajax call aswell Commented Oct 8, 2015 at 12:45
  • If you POSTed your JSON then it would work. Not from within the query string like that though Commented Oct 8, 2015 at 12:46

2 Answers 2

3

This:

http://myServer/myController/ModelTest?input={"num":5, "name ":"myName"}

Is a very invalid URL. Query strings are great for simple key/value pairs, for example:

http://myServer/myController/ModelTest?num=5&name=myName

In fact, for a simple object like your Test class, the model binder should be smart enough to interpret these values into an instance of that object. (I'll be very surprised if it doesn't.)

However, if you have a JSON structure (which you may need for more complex objects) then that structure would have to go in the POST body rather than in the query string.

Sign up to request clarification or add additional context in comments.

3 Comments

In answer to your question, yes MVC is smart enough to bind the query string values to the model like you have stated
Thank you! Another question, shouldn't POST be used to insert things and GET to get things? What if I need to pass a complex serialized object from client but just to request content and not to insert?
@Naigel: Generally, yes. GET is strictly for observing state and not for changing it. There are semantic arguments to be made for POST, though. And the W3C spec leaves room for interpretation (intentionally). A significant difference between the two is that GET has no request body. So if you need to have a request body, POST is going to be your option. POST is generally for creating resources. But there's room for interpretation with operations like complex searches. If it helps, semantically think of it as "creating a set of search results".
1

If you POST your values using AJAX or using a form, it will automatically bind your JSON values to your incoming model in your action method.

Here's an example of jQuery code that you could use to post your JSON:

$.post("http://myServer/myController/ModelTest", {"num":5, "name ":"myName"}, function(e) {

    // Success
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.