Copied to clipboard

Flag this post as spam?

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


  • Irshad 3 posts 73 karma points
    May 25, 2016 @ 12:27
    Irshad
    0

    How to create an instance of a model that inherits RenderModel inside UmbracoApiController?

    Hello Umbraco, I have the following model:

    public class LoyaltyPromo : RenderModel
        {
            public LoyaltyPromo(IPublishedContent content)
                : base(content) // the error points here
            {
            }
            //properties removed for brevity
        }
    

    I want to use this model inside UmbracoApiController. So i do this:

    public class PromoServiceController : UmbracoApiController
        {
            public async Task<object> GetAll()
            {
    
                var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
                IPublishedContent content = umbracoHelper.TypedContent(1050);
                var list = new List<LoyaltyPromo>();
                list.Add(new LoyaltyPromo(content));
            }
        }
    

    Unfortunately it doesn't work, i get the NullReferenceException:

    An error has occurred. Object reference not set to an instance of an object. System.NullReferenceException at Umbraco.Web.Models.RenderModel..ctor(IPublishedContent content) at LoyaltyOps.Models.LoyaltyPromo..ctor(IPublishedContent content) in C:\Users\mkallingal\documents\visual studio 2015\Projects\LoyaltyOps\LoyaltyOps.Models\LoyaltyPromo.cs:line 13 at LoyaltyOps.Controllers.PromoServiceController.d0.MoveNext() in C:\Users\mkallingal\documents\visual studio 2015\Projects\LoyaltyOps\LoyaltyOps\Controllers\PromoServiceController.cs:line 32 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Threading.Tasks.TaskHelpersExtensions.d3`1.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.Controllers.ApiControllerActionInvoker.d0.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.Controllers.ActionFilterResult.d2.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()

    How can i resolve this?

  • Ismail Mayat 4511 posts 10092 karma points MVP 2x admin c-trib
    May 25, 2016 @ 12:59
    Ismail Mayat
    1

    whats on line 13 of LoyaltyPromo?

  • Irshad 3 posts 73 karma points
    May 25, 2016 @ 13:46
    Irshad
    0
    : base(content) 
    

    this is where the error pointing to (line 13)

  • Irshad 3 posts 73 karma points
    May 25, 2016 @ 15:55
    Irshad
    0

    Hi, Here is the screenshot where i'm getting the exception. Any help or hint is much appreciated.

    Here is the screenshot where i'm getting the exception

  • Martin Amsinck Andersen 17 posts 103 karma points
    May 25, 2016 @ 16:42
    Martin Amsinck Andersen
    0

    Why are you using the rendermodel? The renderModel is normally used in the RenderMvcController

    I would create my own model, or use the modelsBuilder class.

Please Sign in or register to post replies

Write your reply to:

Draft