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


i'm using umbraco cms. have following model:

 public class loyaltypromo : rendermodel     {         public loyaltypromo(ipublishedcontent content)             : base(content)         {         }         //properties removed brevity     } 

i want use model inside umbracoapicontroller. 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));                 return list;              }         } 

unfortunately doesn't work, nullreferenceexception:

screenshot exception occurs

error has occurred. object reference not set instance of object. system.nullreferenceexception @ umbraco.web.models.rendermodel..ctor(ipublishedcontent content) @ loyaltyops.models.loyaltypromo..ctor(ipublishedcontent content) in c:\users\mkallingal\documents\visual studio 2015\projects\loyaltyops\loyaltyops.models\loyaltypromo.cs:line 13 @ loyaltyops.controllers.promoservicecontroller.d__0.movenext() in c:\users\mkallingal\documents\visual studio 2015\projects\loyaltyops\loyaltyops\controllers\promoservicecontroller.cs:line 32 --- end of stack trace previous location exception thrown --- @ system.runtime.compilerservices.taskawaiter.throwfornonsuccess(task task) @ system.runtime.compilerservices.taskawaiter.handlenonsuccessanddebuggernotification(task task) @ system.threading.tasks.taskhelpersextensions.d__3`1.movenext() --- end of stack trace previous location exception thrown --- @ system.runtime.compilerservices.taskawaiter.throwfornonsuccess(task task) @ system.runtime.compilerservices.taskawaiter.handlenonsuccessanddebuggernotification(task task) @ system.web.http.controllers.apicontrolleractioninvoker.d__0.movenext() --- end of stack trace previous location exception thrown --- @ system.runtime.compilerservices.taskawaiter.throwfornonsuccess(task task) @ system.runtime.compilerservices.taskawaiter.handlenonsuccessanddebuggernotification(task task) @ system.web.http.controllers.actionfilterresult.d__2.movenext() --- end of stack trace previous location exception thrown --- @ system.runtime.compilerservices.taskawaiter.throwfornonsuccess(task task) @ system.runtime.compilerservices.taskawaiter.handlenonsuccessanddebuggernotification(task task) @ system.web.http.dispatcher.httpcontrollerdispatcher.d__1.movenext()

how can resolve this?

your problem rendermodel not intended used within webapi, has no context regarding front end page @ all. internally rendermodel relying on publishedcontentrequest property of umbracocontext being set properly, doesn't happen webapi.

if need base model on rendermodel, use alternative constructor:

rendermodel(ipublishedcontent content, cultureinfo culture)

and pass in culture content - around need publishedcontentrequest being set, may run other problems further on.

the better approach create view model doesn't rely on rendermodel @ all. since webapi typically serialises model json anyway, lighter can make better.

one other thing - listing getall() doesn't return @ - assume there's no processing going on in actual method requires model based on rendermodel, , you're returning list?


Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -