为什么ASP MVC模型绑定器只接受POST中的JSON?

时间:2017-03-09 09:03:35

标签: javascript ajax asp.net-mvc

每当我使用AJAX发送'GET' JSON.stringify()时,模型值始终为null; 为什么它只能绑定'POST'? 如果可能,我可以使用'GET'并仍将数据绑定到模型吗?

编辑:添加代码示例

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
        }
    });

型号:

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

控制器:

public ActionResult ShowData(shoemodel get)
{
    List<DataGrid> fetch = func.getdata(get);
    return Json(fetch);
}

2 个答案:

答案 0 :(得分:1)

也许您忘记了GET用于查看内容而不更改内容,而POST则用于更改内容。只有在使用Querystring时,Get才能用于更改内容。另一方面,帖子直接发送表格数据。

答案 1 :(得分:0)

HTTP 'GET'方法不支持请求中的正文。通过'GET'发送参数的方式是使用application/x-www-form-urlencoded格式将其附加到网址中。

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