2

-this问题已被固定的目的是通过myself-序列化包含数组

SO我试图使用knockoutJS并测试映射插件和我在与序列化,其包含一个对象的一些问题和array

下面是控制器我和下面的JavaScript,我遇到的问题是无论我尝试(你可以看到注释掉的尝试修复)我不能让儿童数组传递给JavaScript来显示孩子 - {"id":5,"name":"Testing this works","children":"[{},{}]"}是所有通过。

有人能指出我朝着正确的方向请

namespace TestingKnockout.Controllers 
{ 
public class child 
{ 
    int id; 
    String name; 

    public child(int id, string name) 
    { 
     this.id = id; 
     this.name = name; 
    } 
} 
public class HomeController : Controller 
{ 
    public ActionResult Index() 
    { 
     return View(); 
    } 

    public virtual string GetData() 
    { 
     List<child> childrenList = new List<child>(){ 
       new child(2, "bob"), 
       new child(4, "dave") 
     }; 
     var result = new 
     { 
      id=5, 
      name="Testing this works", 
      children = childrenList 
      //children = Newtonsoft.Json.JsonConvert.SerializeObject(childrenList) 
      //children = new{ id = 2, name = "bob" } 

     }; 

     //String resultsToHighlightJSON =     Newtonsoft.Json.JsonConvert.SerializeObject(childrenList); 
     //return resultsToHighlightJSON; 
     return new JavaScriptSerializer().Serialize(result);// +resultsToHighlightJSON; 
    } 
} 
} 

这是我的javascript:

<script language="javascript" type="text/javascript"> 
    var originalData = { 
     id: 1, 
     name: "Main", 
     children: [] 
    }; 

    var updatedData = { 
     id: 1, 
     name: "Main", 
     children: [{ id: 2, name: "bob" }, { id: 3, name: "ted"}] 
    }; 

    function Child(id, name) { 
     this.id = ko.observable(id); 
     this.name = ko.observable(name); 
    } 

    var options = { 
     children: { 
      key: function (data) { 
       return ko.utils.unwrapObservable(data.id); 
      } 
     } 
    } 

    var viewModel = ko.mapping.fromJS(originalData, options); 

    viewModel.counter = 1; 
    viewModel.addChild = function() { 
     viewModel.children.push(new Child(++viewModel.counter, "new")); 
    }; 



    viewModel.applyUpdate = function() { 
    var basePath="<%: Url.Content("~/") %>"; 
    var url = basePath + 'Home/GetData/'; 
    $.get(url, function (response) { 
       var Employee = $.parseJSON(response); 
       $("#EditLink").html("testing this out : " + Employee.children[1].name); 
     //ko.mapping.fromJS(updatedData,viewModel); 
     ko.mapping.fromJS(Employee,viewModel); 
       }); 
    } 

    $(document).ready(function() { 
     ko.applyBindings(viewModel); 
    }); 

+0

您是否已经使用小提琴手来看看什么是真正被返回给客户端JSON? – 2013-02-18 16:00:23

+0

当我使用应用更新功能 – kaldhu 2013-02-18 16:27:35

+0

啊,所有正在传递的是{“id”:5,“name”:“Testing this works”,“children”:“[{},{}]”}不是因为你不能像这样在C#中序列化一个泛型列表?尝试将其更改为数组,然后查看是否有效。 – 2013-02-18 18:45:00

回答

0
public class child 
{ 
    public int id { get; set; } 
    public String name { get; set; } 

    public child(int id, string name) 
    { 
     this.id = id; 
     this.name = name; 
    } 
} 

我的错误是我没有设置公共属性

0

GetData()功能试试这个。

return Newtonsoft.Json.JsonConvert.SerializeObject(result); 

我从来没有JSON.NET的任何问题,我知道它支持匿名类型的事实。

+0

我已经以各种形式尝试过了(正如您可能从我注释过的行中注意到的那样。数组仍然由于某些未知原因而为空) – kaldhu 2013-02-19 10:36:49