2013-03-17 76 views
0

我想發佈一個ko.observable數組作爲對象的一部分,所有數據到達服務器好除了數組不是null,但有一個計數爲零。作爲對象屬性後,可觀察數組asp.net mvc

這是在客戶端

function submitAsync() { 
    var viewModel = constructModel(); 

    setTimeout(function() { 
     $.ajax({ 
      url: '/Article/Index', 
      type: 'POST', 
      data: JSON.stringify({ viewModel: viewModel }), 
      contentType: 'application/json; charset=utf-8', 
     }) 
    },2000); 
    console.log(viewModel); 
} 

function constructModel(){ 
    var articleViewModel = {}; 
    articleViewModel.Authors = ko.toJSON(appViewModel.authors); 

    articleViewModel.ArticleData = {}; 
    articleViewModel.ArticleData.Title = $("#ArticleData_Title").text(); 
    articleViewModel.ArticleData.CorespondingAuthor = $("#ArticleData_CorespondingAuthor").text(); 
    articleViewModel.ArticleData.Keywords = $("#ArticleData_Keywords").text(); 

    articleViewModel.ArticleContent = {}; 
    articleViewModel.ArticleContent.Abstract = $("#ArticleContent_Abstract").text(); 
    articleViewModel.ArticleContent.FullText = ArticleContent(); 

    return articleViewModel; 
} 

我的視圖模型

public class ArticleViewModel 
{ 
    public ArticleData ArticleData { get; set; } 
    public ArticleContent ArticleContent { get; set; } 
    public ICollection<Author> Authors { get; set; } 
} 

我的控制器動作 viewModel.Authors不爲空,但有0

計數
[HttpPost] 
     public ActionResult Index(ArticleViewModel viewModel) 
     { 
      if (ModelState.IsValid) 
      { 
       mergeViewModelToCurrentArticle(viewModel); 
       _documentPath = GenerateDocument(_currentArticle); 
       return RedirectToAction("Success"); 
      } 
      return View(); 
     } 

的KO陣列從JavaScript

Authors: "[{"id":1,"firstName":"User first name","lastName":"user last name","email":"[email protected]","phone":"xxxxx","address":"Dunarii Nr.3","fullName":"user full name"}]" 
+0

怎樣一個'Author'看起來像在C#中?您也可以嘗試使用數據:'JSON.stringify(viewModel),' – nemesv 2013-03-17 17:11:04

+1

屬性名稱的外框應該匹配。在JS中你有'firstName',但在C#'FirstName'中...... – nemesv 2013-03-17 17:17:14

+0

沒有JSON.stringify我得到無效的JSON原始代碼:作者。 – trebor 2013-03-17 17:18:49

回答

1

由於達林我設法弄明白顯然的解決辦法是更換

articleViewModel.Authors = ko.toJSON(appViewModel.authors); 

articleViewModel.Authors = $.parseJSON(ko.toJSON(appViewModel.authors)) 
+0

這工作對我來說,但我殺了我的約會。 – Kieran 2013-03-26 04:34:02

1

outputed只需更換

articleViewModel.Authors = ko.toJSON(appViewModel.authors); 

與:

articleViewModel.Authors = appViewModel.authors; 

您是編碼的著者陣列是沒有必要的雙JSON。

+0

想到了這一點,並且已經嘗試過了,但是如果我這樣做了作者現在變爲null,而不是計數爲0 – trebor 2013-03-17 17:41:50

+0

嘗試從'Author'模型中去除'implicit operator'。 – 2013-03-17 17:43:36

+0

這也沒有效果,並用於工作時,我只是發佈作者,即使與隱式運算符 – trebor 2013-03-17 17:50:48

相關問題