I updated the UpshotHelper nuget package a few weeks ago.  The update included an abstract Controller to handle submissions from Upshot.js.

To demonstrate the usage of the UpshotHelper I forked Steve Sanderson’s DeliveryTracker from his ASP.NET Single Page Application presentation.  You can review the source code here.  

UpshotHelper – UpshotContext

The first part in using UpshotHelper is in initializing upshot.js.  UpshotHelper will generate the JavaScript for your Data Provider and object metadata.  Here is the Home Index.cshtml from DeliveryTracker.

@using DeliveryTracker.Controllers
@using DeliveryTracker.Models
@section featured {

}
@(Html.UpshotContext(bufferChanges: true).DataSource(x => x.GetDeliveriesForToday(), typeof(Delivery)))






  1. for

Customers

  • Name:

UpshotHelper – UpshotController

The second part is configuring your Controller.  The UpshotHelper includes an abstract ApiController called UpshotController.  You will need to inherit the UpshotController to automatically handle the Submit action and JSON ChangeSet serialization that Upshot.js sends to the configured Data Provider.  Here is the DataServiceController from DeliveryTracker.

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.Validation;
using System.Data.Objects;
using System.Linq;
using DeliveryTracker.Models;
using UpshotHelper.Controllers;
using UpshotHelper.Models;

namespace DeliveryTracker.Controllers
{
    public class DataServiceController : UpshotController 
    {
        private AppDbContext _dbContext;

        /// 
        /// Initializes a new instance of the  class.
        /// 
        public DataServiceController()
        {
            _dbContext = new AppDbContext();
        }

        /// 
        /// Gets the deliveries for today.
        /// 
        /// Returns the Deliveries for today.
        public IQueryable GetDeliveriesForToday()
        {
            // Could pre-filter by due date, delivery driver, etc...
            return _dbContext.Deliveries.Include("Customer").OrderBy(x => x.DeliveryId);
        }

        /// 
        /// Processes the submit.
        /// 
        /// The change set.
        /// Returns True if successful, otherwise false.
        protected override bool ProcessSubmit(ChangeSet changeSet)
        {
            bool success = true;

 try
 {
 foreach (ChangeSetEntry entry in changeSet.ChangeSetEntries)
 {
                    switch (entry.Operation)
                    {
                        case ChangeOperation.Update:
                            DbEntityEntry deliveryEntry = _dbContext.Entry(entry.Entity);

                            Customer customer = _dbContext.Customers.Local.FirstOrDefault(c => c.CustomerId == (deliveryEntry.Entity as Delivery).CustomerId);
                            if (customer != null)
                            {
                                (deliveryEntry.Entity as Delivery).Customer = customer;
                            }

                            deliveryEntry.State = EntityState.Modified;
                            _dbContext.SaveChanges();
                            break;
                    }
 }

 }
 catch (Exception ex)
 {
 success = false;
 }

 return success;
        }
    }
}

If you have any questions or  just want to let me know what you think, please leave a comment.