我想發佈一個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"}]"
怎樣一個'Author'看起來像在C#中?您也可以嘗試使用數據:'JSON.stringify(viewModel),' – nemesv 2013-03-17 17:11:04
屬性名稱的外框應該匹配。在JS中你有'firstName',但在C#'FirstName'中...... – nemesv 2013-03-17 17:17:14
沒有JSON.stringify我得到無效的JSON原始代碼:作者。 – trebor 2013-03-17 17:18:49