Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6957209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:58:14+00:00 2026-05-27T14:58:14+00:00

I am working through a KnockOutJs Sample and am having some posting problems with

  • 0

I am working through a KnockOutJs Sample and am having some posting problems with MVC3. My sample correctly posts when using a full page postback. When I try to save using an jQuery Ajax post, I can see the Post in the using Firebug NET viewer to be:

{ gifts:[{“GiftId”:0,”Title”:”sad”,”Price”:3}] }

When I view the ControllerContext in the ModelBinder in MVC3, the form parameters are empty and the json does not bind. Any ideas as to what is happening?

I have tried a number of configurations but here is the jQuery posting code (currently hard-coded to a static value):

…

            $.ajax({
        url: "/Home/PartialUpdate", 
        type: 'POST', 
        cache: false,
        data:   '{ gifts:[{"GiftId":0,"Title":"sad","Price":3}] }', //ko.toJSON({ gifts: this.gifts }),   
        dataType: 'json' ,
        contentType: "application/json;",
        success: function(result){
            alert(result);
            var data = ko.utils.parseJson(result); 
            this.gifts =  ko.observableArray(data) ;
        },
        error:function(xhr,err){ 
            alert("readyState: " + xhr.readyState+"\nstatus: "+xhr.status); 
            alert("responseText: " + xhr.responseText);
        }
        });

Edit: Here is the MVC3 action code for the Ajax update code

[HttpPost]
public JsonResult PartialUpdate ([FromJson] IEnumerable<Gift> gifts)
{
    gifts = gifts ?? new List<Gift>();
    using (var context = new KnockOutContext())
    {
        // Add record if not in DB
        foreach (var gift in gifts )
        {
            context.Entry(gift).State = (gift.GiftId == 0) ? EntityState.Added : EntityState.Modified;
        }

        // Delete records if not in ViewModel
        foreach (var dbGift in context.Gifts)
        {
            if (gifts.SingleOrDefault(c => c.GiftId == dbGift.GiftId) == null)
                context.Gifts.Remove(dbGift);
        }
        context.SaveChanges();
    }
    return GetGifts_Json();
}

And the full postback code that works (from Steve Sanderson’s example at
http://blog.stevensanderson.com/2010/07/12/editing-a-variable-length-list-knockout-style/)

[HttpPost]
public ActionResult Index([FromJson] IEnumerable<Gift> gifts)
{
    SaveGifts(gifts);
    return RedirectToAction("Index");
}

using this custom model binder:

        public class FromJsonAttribute : CustomModelBinderAttribute
        {
            private readonly static JavaScriptSerializer serializer = new JavaScriptSerializer();

            public override IModelBinder GetBinder()
            {
                return new JsonModelBinder();
            }

            private class JsonModelBinder : IModelBinder
            {
                public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
                {
                    var stringified = controllerContext.HttpContext.Request[bindingContext.ModelName];
                    if (string.IsNullOrEmpty(stringified))
                        return null;
                    return serializer.Deserialize(stringified, bindingContext.ModelType);
                }
            }
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T14:58:14+00:00Added an answer on May 27, 2026 at 2:58 pm

    My guess is that you started with Steve’s download, which is using MVC2. MVC2 did not have the JsonValueProvider registered by default. The [FromJson] attribute was intended to work with URL-encoded JSON that was submitted via ko.utils.postJson (full postback). This is not necessary when posting JSON via AJAX with the correct content-type (in MVC3).

    So, the easiest thing to do is upgrade your project to MVC 3 (easy way here) and remove the [FromJson] attribute from your partial update.

    Working copy here.

    One other really minor thing: your static data is currently invalid JSON ('{ gifts:[{"GiftId":0,"Title":"sad","Price":3}] }'). gifts would need to be "gifts"

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Working through some OpenGL-ES tutorials, using the Android emulator. I've gotten up to texture
I've been working through problems on Project Euler , and some of the solutions
I am working through some C++ code from Financial Instrument Pricing Using C++ -
I'm working through some python problems on pythonchallenge.com to teach myself python and I've
Still working through JQuery to get data and repopulate portions of a page. Right
I'm working through previous years ACM Programming Competition problems trying to get better at
I'm working through some homework and a question on a previous exam paper asks
I am working through some of the exercises in The C++ Programming Language by
Working through a sample in Chapter 3 of Programming in Scala, the following code
While working through Real World Haskell, I tried to complete the palindrome exercise using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.