Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • aliyu 1 post 71 karma points
    Sep 20, 2018 @ 09:42
    aliyu
    0

    TypedContent returning null for newly created content from a class that inherits from SurfaceController

    I have a method in a class that inherits from SurfaceController which creates new content then calls TypedContent on the freshly created and published content, TypedContent returns null.

    It used to work fine before, now it just stopped. Also, a near identical method in a class that inherits from UmbracoApiController still works.

    I did move the db around a bit, but have republished whole site and used the Examine tools, still does not work.

    [HttpPost]
        public ActionResult TakeExam(int umbracoId)
        {
            var helper = new UmbracoHelper(UmbracoContext);
            var exam = new ExamVm(helper.TypedContent(umbracoId));
            var ct = Services.ContentTypeService.GetContentType("memberProxy");
            var proxyPage = Services.ContentService.GetContentOfContentType(ct.Id)
                .First(c => c.Name == "[email protected]");
            proxyPage= Services.ContentService.GetById(proxyPage.Id);
    
    
            var script = Services.ContentService.CreateContent(
                exam.Title + "-" + DateTime.Now.Ticks, 
                proxyPage.Id, 
                UmbracoAlias.examScript.ToString(), 
                0);
    
    
            script.SetValue("status", Helpers.Helpers.GetStatusId(ScriptStatus.InProgress));
            script.SetValue("startTime", DateTime.Now);
            script.SetValue("isMarked", false);
            script.SetValue("exam", exam.Content.ToUdiCsv());
    
    
            Services.ContentService.SaveAndPublishWithStatus(script);
    
    
            var questNo = 1;
            foreach (var examSectionVm in exam.Sections)
            {
                Random rng = new Random();
                var shuffQuests = examSectionVm.Questions.OrderBy(a => rng.Next()).ToList();
                var i = 1;
                foreach (var shuffQuest in shuffQuests)
                {
                    var ans = Services.ContentService.CreateContent(
                        shuffQuest.Name, 
                        script.Id, 
                        "scriptAnswer", 
                        0);
    
                    ans.SetValue("question", shuffQuest.Content.ToUdiCsv());
                    ans.SetValue("questionNumber", questNo++);
                    ans.SetValue("isAnswered", false);
    
                    Services.ContentService.SaveAndPublishWithStatus(ans);
    
                    //i++;
                    if (i++ == examSectionVm.NoOfQuestions)
                        break;
                }
            }
    
            /////THIS IS WHERE IT FAILS!!!!
            var scriptVm = new ExamScriptVm(Umbraco.TypedContent(script.Id));
            var quest1Vm = scriptVm.Answers.First();
    
    
            return RedirectToUmbracoPage(scriptVm.UmbracoId);
        }
    

    Umbraco.TypedContent works with older data, it just doesn't work with any new data.....until I view the data in the backend control panel. After viewing it there, even as just a member of a list, it starts working.

    I'm sure I'm missing something obvious here...

    Any ideas? thanks!

    (Also aware this isn't the most optimized or clever code out there, but I just need this to work for now...)

Please Sign in or register to post replies

Write your reply to:

Draft