0

Whenever I send 'GET' with JSON.stringify() using AJAX, model value is always accept null; Why it can only bind 'POST'? If it is possible, can I use 'GET' and still bind data to model?

Edit: adding Code Example

JS:

$.ajax({
        var modelsend = {
            itemname: 'shoe',
            itemcolor: 'red',
            itemsize: '31',
            itemvariety: 'SR-31',
        }
        type: "POST",
        url: "@Url.Action("ShowData", "Controller")",
        data: JSON.stringify(modelsend),
        dataType: "json",
        contentType: "application/json",
        success: function (data) {
            //do something with data
        },
        error: function (jqXHR, textStatus, errorThrown) {
            //show error
        }
    });

Model:

public class shoemodel
{
    public string itemname { get; set; }
    public string itemcolor { get; set; }
    public string itemsize { get; set; }
    public string itemvariety { get; set; }
}

Controller:

public ActionResult ShowData(shoemodel get)
{
    List<DataGrid> fetch = func.getdata(get);
    return Json(fetch);
}
KokoriNut
  • 175
  • 1
  • 3
  • 11
  • Yes it is possible. Show your ajax call and controller with model – teo van kot Mar 09 '17 at 09:04
  • I assume your also setting `contentType: 'json` - a GET does not have a body. You can bind fine to the GET method so long as the data is constructed correctly –  Mar 09 '17 at 09:05
  • yeah, my ajax using contentType: "application/json". Ok, so GET only send url without any data, but POST send body which is formatted in JSON. Whenever I send GET, body is empty and I always get null, isn't it? It doesn't matter what is my contentType if I send GET because I don't send 'content' afterall? – KokoriNut Mar 09 '17 at 09:14
  • @KokoriNut, You need to give an example of what your sending (and the model you want to bind to). It will bind fine if its in the correct format. –  Mar 09 '17 at 09:25
  • All your need is `type: "get",` and `data: modelsend,` (and delete `contentType: "application/json",`) –  Mar 09 '17 at 09:28
  • Now your posting to a method which has a model `DataMemberVM` - it needs to be `shoemodel` since that is what your passing in the request (and your url does not make sense - its `url: /controller/ShowData,` –  Mar 09 '17 at 09:36
  • I'm sorry because it's not real action and controller name. Yeah, you are right. It bind just fine.For 'GET' method , I should not declare contentType? – KokoriNut Mar 09 '17 at 09:38
  • There is no point in setting `contentType` in a GET (its just ignored if you do) –  Mar 09 '17 at 09:40
  • Found this http://stackoverflow.com/questions/5661596/do-i-need-a-content-type-for-http-get-requests – KokoriNut Mar 09 '17 at 09:53
  • @KokoriNut, Not sure what you think that has to do with your question. Setting the `contentType` is simply ignored by the `DefautModelBinder` when its a GET –  Mar 09 '17 at 10:11
  • Somewhat derailed, My answer about model binder has already been answered, and so the question about if I should or not setting contentType. Just provide more information case of people like me who wondering if contentType should be declared even if it has no effect or whatsoever. It is not relevant though... – KokoriNut Mar 09 '17 at 10:22

2 Answers2

1

Perhaps you are forgetting that GET is used for viewing something, without changing it, while POST is used for changing something. And Get can be used to change something only when you use Querystring. Post on the other hand sends form data directly.

rexroxm
  • 868
  • 1
  • 9
  • 26
  • I am viewing something, but with complex searching, I prefer binding my search parameter to Model. concate more than 40 parameter to url is too taxing, and receive it is a pain in the **** – KokoriNut Mar 09 '17 at 09:27
  • Notice that the content you send is of the type "application/json", you are POSTING the object modelsend which is the way you send data while using post method. In get method you do post he data but with qurystring. – rexroxm Mar 09 '17 at 09:41
0

HTTP 'GET' method doesn't support a body in the request. The way to send parameters via 'GET' is using the application/x-www-form-urlencoded format appending them in the URL like this.

http://example.com/?key1=value1&key2=value2

jorgonor
  • 1,679
  • 10
  • 16