Question

I am new to BreezeJS, and have recently created a project that is up and running. I am able to use Breeze to query data from my SQL server without a problem. However, whenever I try to save changes, the changes do not save. I have verified, in VS 2012, that the JavaScript save function is actually called, however the HttpPost method on the server side is never called.

Here is the Breeze Controller that I have set up:

    using System.Linq;
using System.Web.Http;
using Newtonsoft.Json.Linq;
using Breeze.WebApi;
using FitnessTracker.Models;

namespace FitnessTracker.Controllers
{
    [BreezeController]
    public class BreezeController : ApiController
    {
        private readonly EFContextProvider<FitnessTrackerContext> _fitnessContext =
            new EFContextProvider<FitnessTrackerContext>();
        //
        // GET: /Breeze/

        [HttpGet]
        public string Metadata()
        {
            return _fitnessContext.Metadata();
        }

        [HttpPost]
        public SaveResult SaveChanges(JObject saveBundle)
        {
            return _fitnessContext.SaveChanges(saveBundle);
        }

        [HttpGet]
        public IQueryable<FitnessEquipment> FitnessEquipments()
        {
            return _fitnessContext.Context.FitnessEquipments;
        }

    }
}

Here is the save function that I am using:

function saveChanges() {
        return manager.saveChanges()
            .fail(saveFailed);
}

Here is the HTML:

<div class="box-body form" data-bind="foreach: exercises">
    <input type="text" class="txt-m g2" data-bind="text: EquipmentName" />
    <input type="text" class="txt-m g1" data-bind="text: EquipmentLevel" />
    <input type="text" class="txt-m g2" data-bind="text: DurationSeconds" />
    <input type="text" class="txt-m g2" data-bind="text: Weight" />
    <input type="text" class="txt-m g4" data-bind="text: DateTime" />
    <br class="clear" />
</div>

Save Changes

Thanks in advance for your help!!

Edit 9/12/2013: I have fixed it thanks to Jay's help!!

The problem is that I was binding on the text instead of the value. I changed the html to:

<div class="box-body form" data-bind="foreach: exercises">
    <input type="text" class="txt-m g2" data-bind="value: EquipmentName" />
    <input type="text" class="txt-m g1" data-bind="value: EquipmentLevel" />
    <input type="text" class="txt-m g2" data-bind="value: DurationSeconds" />
    <input type="text" class="txt-m g2" data-bind="value: Weight" />
    <input type="text" class="txt-m g4" data-bind="value: DateTime" />        
    <br class="clear" />
</div>

It is now saving without a problem. Thanks Jay for your help!

Was it helpful?

Solution

Have you confirmed that you are actually changing/or adding an entity within the EntityManager on the client? You can call the EntityManager's hasChanges method to confirm. Breeze does not attempt to call the server if there are no changes to save.

So try something like

if (manager.hasChanges()) {
   manager.saveChanges()
           .fail(saveFailed);
} else {
   // my guess is that you will get here.
}

My guess is that you either have a binding issue or have not queried or created an entity that requires saving.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top