0

I'm trying to post an ko.observable array as part of of an object, all that data reaches the server ok except for the array which is not null but has a count of zero.

This is on the client side

    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;
}

My viewModel

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

My controller action viewModel.Authors is not null but has a count of 0

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

The ko array outputed from 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"}]"
4
  • How is one Author looks like in C#? You can also try with data: JSON.stringify(viewModel), Commented Mar 17, 2013 at 17:11
  • 1
    The casing of the property names should match. In JS you have firstName but in C# FirstName... Commented Mar 17, 2013 at 17:17
  • without JSON.stringify i get Invalid JSON primitive: Authors. Commented Mar 17, 2013 at 17:18
  • i've updated it now the data i post looks like Authors: "[{"Id":1,"FirstName":"First Name","LastName":"LastName","Email":"[email protected]","Phone":"0751000000","Address":"Dunarii Nr.3","fullName":"First Name LastName"}]" Commented Mar 17, 2013 at 17:33

2 Answers 2

1

Simply replace

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

with:

articleViewModel.Authors = appViewModel.authors;

You are double JSON encoding the Authors array which is not necessary.

Sign up to request clarification or add additional context in comments.

9 Comments

Thought about that, and tried it already but if i do that Authors now becomes null, instead of having a count of 0
Try removing the implicit operator from your Author model.
That didn't work either and is used to work when i posted just the authors, even with the implicit operator
That's weird. Could you please show the exact JSON that gets sent to the server during the AJAX request? I suspect that there might be some discrepancy between your model and the JSON string.
Don't really think so because as i said it worked when i was just posting the authors, but i'm really new at this. Here's the JSON
|
1

Thanks to Darin i managed to figure it out apparently the solution was to replace

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

with

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.