ko.toJSON

瀏覽次數:45 次
跳到第一則未讀訊息

shr...@gmail.com

未讀,
2015年4月28日 晚上9:18:272015/4/28
收件者:knock...@googlegroups.com
the server model:
public class BT
    {
        public IList<B> Bs{ get; set; }
        public IList<T> Ts{ get; set; }
    }
    public class B
    {
        [Key]
        public int BID { get; set; }
        [Required]
        public string BText { get; set; }
    }
    public class T
    {
        [Key]
        public int TID { get; set; }
        [Required]
        public string TText { get; set; }
    }

in client I want to return the BT object to server.
function BViewModel() {
 
    var self = this;
  
    self.Bs = ko.observableArray([]);
    self.newB = ko.observable("");


    self.Ts = ko.observableArray([]);
    self.newT = ko.observable("");


    self.b= { Bs: self.Bs, Ts: self.Ts};
 
    // Operations
    self.save = function () {      

        $.ajax("/board/Save", {
    
            data: ko.toJSON({ BT: self.b} ),
            type: "post",
            contentType: "application/json",
            success: alert('success');      
        });   
    };
}
ko.applyBindings(new BViewModel());

and the server method:
        public ActionResult Save(BT b)
        {
            return view();
        }

how should I make    self.b= { Bs: self.Bs, Ts: self.Ts}; so the server recognize that? it is null.

saratan...@gmail.com

未讀,
2015年4月29日 下午3:11:192015/4/29
收件者:knock...@googlegroups.com、shr...@gmail.com
To get data back to server change your ajax call as below:
---------------------------------------------------
$.ajax("/board/Save", {
            type: "post",
            data: { 
              "BT": ko.toJS(self.b)
            },
            success: alert('success');
        });
---------------------------------------------------
回覆所有人
回覆作者
轉寄
0 則新訊息