【问题标题】:Json string list is coming with emptyJson 字符串列表是空的
【发布时间】:2012-04-20 07:39:59
【问题描述】:

我正在将一个 json 字符串集合从 Android 设备传递到 .net MVC HttpPost 方法。我的 json 字符串是这样的。

{"collection",[{"Name":"A","Age":"12","Class":"10"},{"Name":"B","Age":"12","Class":"10"}]}

我的MVC控制函数是:

  [HttpPost]
    public ActionResult Create(string[] collection)
    {
        try
        {
            // TODO: Add insert logic here
            JavaScriptSerializer json_serializer = new JavaScriptSerializer();
            List<Model.StudentBehaviour> stdbehaviour_list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Model.StudentBehaviour>>(collection);
            Lib.StudentModule.StudentManager.InsertStudentBehaviours(stdbehaviour_list);               
            return Json("success", JsonRequestBehavior.AllowGet);
        }
        catch
        {
            return Json("exception", JsonRequestBehavior.AllowGet);
        }
    }

函数参数值为

collection = "(Collection)"

集合中的预期值为

collection[0] 
Name = A 
Age = 12
Class = 10
collection[1] 
Name = B 
Age = 12
Class = 10

请帮忙解决这个问题

提前致谢

【问题讨论】:

    标签: asp.net-mvc json http-post


    【解决方案1】:

    ASP.NET MVC 有 ModelBinding 的概念。这意味着如果您向 Action 方法添加参数,MVC 将尝试用您发送的数据填充这些参数。

    这意味着您可以将代码更改为:

     // Example of your Student class. 
     // Make sure that all properties you want to bind to are public
     public class Student
     {
         public string Name { get; set; }
         public int Age { get; set; }
         public int Class { get; set; }
     }
    
    // Example of an Action method. Note that instead of taking a string as parameter,
    // you just accept a collection of Student objects.
    [HttpPost]
    public JsonResult Create(List<Student> collection)
    {
        if (ModelState.IsValid)
        {
            return Json("success", JsonRequestBehavior.AllowGet);
        }
        else
        {
            return Json("exception", JsonRequestBehavior.AllowGet);
        }
    }
    

    然后您可以使用以下 jquery 发布您的数据:

    function sendData() {    
            var data = [{ 'Name': 'A', 'Age': '12', 'Class': '10' },
                { 'Name': 'B', 'Age': '12', 'Class': '10' }];
    
            var collection = JSON.stringify(data);
    
            $.ajax({
                url: "Home/Create",
                type: "POST",
                data: collection,
                dataType: 'json',
                contentType: 'application/json; charset=utf-8',
            }).done(function (msg) {
                alert("Data Saved: " + msg);
            });
        }
    

    【讨论】:

    • 感谢 Wouter de Kort,我会尝试这种方法,我有一个简单的问题要问你参数名称和 Json 密钥应该相同吗?例如:如果我通过 {"Details": [{ 'Name': 'A', 'Age': '12', 'Class': '10' }, { 'Name': 'B', 'Age': '12', '类': '10' }] 。但是我的 HttpPost 函数参数名称是 collection 有什么问题吗?
    • 是的,它们应该是一样的。 ModelBinding 用于将参数名称与您的请求中找到的数据相匹配。
    • 嗨 Wouter de Kort,我实现了你给出的代码。但仍然是 null 。我必须在 Global.ascx 中做的任何事情。
    • 嗨,如果我有一个像 {"collection",{"Name":"A","Age":"12","Class":"10" }}。但如果我使用的是 am 列表,那么会得到一个空对象。
    • 您不必更改 global.ascx 中的任何内容。我已经在一个新的默认 MVC 项目中测试了这段代码。如果将参数类型从 List 更改为字符串并发送多个对象,您会得到什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-11
    • 1970-01-01
    • 1970-01-01
    • 2011-04-20
    • 1970-01-01
    • 1970-01-01
    • 2013-01-15
    相关资源
    最近更新 更多