Correct way to Update a record with IContentManager?

Topics: General, Troubleshooting, Writing modules
Feb 25, 2015 at 3:10 AM
Hey guys, for the life of me, I can't update a row in my DB using IContentManager- all the other code works fine: [NOTE: I'm trying to extend Duemetri.Activities [FullCalendar.js] module.]
       [Admin]
        public ActionResult Create()
        {
            if (!_services.Authorizer.Authorize(ActivityPermissions.ManageActivity, T("Not authorized"))) {
                return new HttpUnauthorizedResult();
            }

            var activity = _services.ContentManager.New("Activity");
            if (activity == null) {
                return HttpNotFound();
            }

            var model = _services.ContentManager.BuildEditor(activity);
            // Casting to avoid invalid (under medium trust) reflection over the protected View method and force a static invocation.
            return View((object) model);
        }

        [HttpPost, ActionName("Create")]
        [Admin]
        public ActionResult CreatePOST()
        {
            if (!_services.Authorizer.Authorize(ActivityPermissions.ManageActivity, T("Not authorized"))) {
                return new HttpUnauthorizedResult();
            }

            var activity = _services.ContentManager.New("Activity");
            _contentManager.Create(activity);
            var model = _services.ContentManager.UpdateEditor(activity, this);

            if (!ModelState.IsValid) {
                _services.TransactionManager.Cancel();

                // Casting to avoid invalid (under medium trust) reflection over the protected View method and force a static invocation.
                return View((object) model);
            }

            _services.Notifier.Information(T("Activity succesfully created"));
            return RedirectToAction("List");
        }
        //TEST CODE
        public bool SaveActivity(string Title, string NewEventDate, string NewEventTime, string NewEventDuration)
        {
            try {
                var activity = _services.ContentManager.New<ActivityPart>("Activity");
                activity.Title = Title;
                activity.DateTimeStart = DateTime.ParseExact(NewEventDate + " " + NewEventTime, "dd/MM/yyyy HH:mm", CultureInfo.InvariantCulture); //new DateTime(2015, 2, 22);
                //activity.DateTimeEnd = new DateTime(2015, 2, 27);
                activity.Url = "test";
                activity.Category = "FF66FF";
                activity.ShowInHome = false;

                _contentManager.Create(activity);
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }
but when it comes to updating, I'm clearly going about it the wrong way:
        public void UpdateActivity(int id, string NewEventStart, string NewEventEnd) {
            var activity = _contentManager.Get<ActivityPart>(11); //A record with primary key 11 exists, but this statement always returns null?
...
        }
ultimately, I wish to update an events DateTime via NewEventStart where the Id's match, something like:
var activity = _services.ContentManager.Query<ActivityPart, ActivityPartRecord>().Where(x => (id == x.Id));
...but clearly i'm still not quite grasping the fundamentals :( If anyone could point me in the right direction that would be great. Thanks for your time, Liv
Feb 25, 2015 at 8:31 AM
Ahh...this solved it:
var activity = _contentManager.Get(id).As<ActivityPart>();
Thanks for your time anyway's ;) Liv