2013-04-20 56 views
2

我無法提交敲除js的表單。傳遞一個函數,在提交過程中返回ko.computed錯誤的值

我收到錯誤「傳遞函數返回ko.computed的值。」

的代碼如下:

(function(records,$,undefined){ 
     records.models={ 
      student:function(data){ 
       var self=this; 
      self.id=ko.observable(data.id); 
      self.fname=ko.observable(data.fname); 
      self.lname=ko.observable(data.lname); 
      if(data.initial==='undefined'||data.initial===null){ 
       self.initial=ko.observable(""); 
      }else{ 
       self.initial=ko.observable(data.initial); 
      } 
      self.fullname=ko.computed(function(){ 
       return self.fname()+" "+" "+self.initial()+" "+self.lname(); 
      }); 
     }, 
     students_model:function(){ 
      var self=this; 
      self.selectedStudent=ko.observable(); 
      self.students=ko.observableArray([]); 
      getStudents();    

      self.save=function(){ 
       var form=$("#student-form"); 
       $.ajax({ 
        type:"POST", 
        url:"/Student/create", 
        data:ko.toJSON(form[0]), //This line here is the exact point of failue 
        success:function(response){ 
         records.general.handleSuccess(response); 
         if(response.status){ 
          getStudents(); 
         }  
        } 

       }); 
       return false; 
      }; 
      function getStudents(){ 
       $.getJSON("/Student/data",function(result){ 
        var mapped=$.map(result,function(item){ 
         return new records.models.student(item);}); 
        self.students(mapped); 
       }); 
      } 
     } 
    }; 
    return records; 
}(window.records=window.records||{},jQuery)); 

HTML

@using (Ajax.BeginForm("Create", "Student", 
new AjaxOptions 
{ 
    HttpMethod = "Post" 
}, 
new { @class = "student-form", name = "student-form", id = "student-form" })) 
{ 
<input type="text" data-bind="value:$root.fname" id="student.fname" name="student.fname" /> 
<input type="text" data-bind="value:$root.lname" id="student.lname" name="student.lname"/> 
<input type="text" data-bind="value:$root.initial" id="student.initial" name="student.initial"/> 
<input type="text" data-bind="value:$root.dob" id="dob" name="dob" /> 
<button data-bind="click:save">Save</button> 
} 

<script type="text/javascript"> 
ko.applyBindings(new records.models.students_model()); 
</script> 

什麼我錯在這裏做什麼?我在這裏意識到這個問題:Pass a function that returns the value of the ko.computed 但似乎這個人有一個不同的問題。我的代碼在保存方法中啓動時失敗。具體線路:

data:ko.toJSON(form[0]) 
+0

你會在哪一行發生錯誤?你的html是怎麼樣的? – nemesv 2013-04-20 09:09:11

+0

我已經更新了這個問題。如果您需要更多信息,請與我們聯繫。 – user1819688 2013-04-20 18:25:02

回答

3

ko.toJSON期待你通過它你的視圖模型,但你把它傳到了DOM,因此錯誤的元素。

你需要傳遞一個javascript對象(視圖模型或你的viewmodel的一部分)到ko.toJSON。例如,如果你想送了學生的數組,你可以這樣做:

ko.toJSON(self.students); 

我看到你的窗體有必然$root.fname$root.lname$root.initial$root.dob一些投入,但我不確定視角模型中存在的位置,所以我無法確切地告訴你要傳遞什麼。但我可以給你一個可以解決這個問題的例子。

如果你有一個視圖模型,看起來像這樣:通過調用

ko.applyBindings(vm); 

var data = ...; 
var vm = { 
    newStudent : { 
    fname : ko.observable(data.fname), 
    lname: ko.observable(data.lname), 
    initial: ko.observable(data.initial ?? ""), 
    dob: ko.observable(data.dob) 
    } 
} 

,然後你束縛,這對你的DOM然後,您可以撥打ko.toJSON這樣的:

... 
data:ko.toJSON(vm.newStudent), 
...