简体   繁体   English

如何将JSON数据发送到MVC控制器

[英]how to send json data to mvc controller

my ajax code is : 我的ajax代码是:

  $.ajax({
            url: "/Dashboard/filter",
            data: jsonData,
            traditional: true,
            contentType: 'application/json',
            dataType: 'json',
            type: "POST",
            success: function (data) {
                console.error(data);

            },
            error: function (error) {
                console.error(error);
            }
        });

and my controller action : 和我的控制器动作:

    [HttpPost]
    public JsonResult filter(Utility.FJson filterjson)
    {

        return Json(new { });
    }

in Utility class: 在实用程序类中:

 public class FilterJson
    {
        public string IdField { get; set; }
        public string NameField { get; set; }
        public int Check { get; set; }
        public string SFilter { get; set; }
        public string TFilter { get; set; }
    }
    public class FJson
    {
        public List<FilterJson> filter { get; set; }

    }

error massage is : 错误消息是:

POST http://localhost:38064/Dashboard/filter 500 (Internal Server Error) POST http:// localhost:38064 /仪表板/过滤器 500(内部服务器错误)

i want send json to mvc controller. 我想将json发送到mvc控制器。 if JSON.stringify(jsonData) in send time .in controller filterjson variable is NULL 如果发送时间中的JSON.stringify(jsonData)。在控制器中的filterjson变量为NULL

You can use the following code to send and receive Json in the form of a class 您可以使用以下代码以类的形式发送和接收Json

Ajax code is: Ajax代码是:

 var data = "{filter :[{IdField: '2', NameField: 'filter item', Check: 1 , SFilter: 'filter search' , TFilter: 'filter text'}]}";
    $.ajax({
        url: '/home/filter',
        dataType: 'json',
        type: 'POST',
        data: data,
        contentType: 'application/json; charset=utf-8',
        success: function (data) {
            alert(data);
        },
        error: function () {
            alert("error");
        }
    });

Class: 类:

public class FilterJson
{
    public string IdField { get; set; }
    public string NameField { get; set; }
    public int Check { get; set; }
    public string SFilter { get; set; }
    public string TFilter { get; set; }
}
public class FJson
{
    public List<FilterJson> filter { get; set; }

}

Controller action is: 控制器动作是:

[HttpPost]
public JsonResult filter(FJson filterjson)
{

    return Json(new { });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM