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 Welcome to my Single Page Application
(x => x.GetDeliveriesForToday(), typeof(Delivery)))
- 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 public DataServiceController() { _dbContext = new AppDbContext(); } ///class. /// /// Gets the deliveries for today. /// ///Returns the Deliveries for today. public IQueryableGetDeliveriesForToday() { // 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.
Related Posts
May 19, 2014
RESTful API
March 3, 2014
WordPress Theme Tips
January 22, 2014
TeamCity with SQL Server
November 20, 2013
TeamCity, Git, and Assembly Version Number
January 22, 2013
Exploring PhantomJS
July 26, 2012
UpshotHelper
May 5, 2012
NHibernate One Shot Delete
April 24, 2012
Database Performance Tuning with Hardware
April 14, 2012