c# - System.NullReferenceException creating viewModel -
so, i'm trying find umbraco node (as ipublishedcontent), , pass viewmodel (as Ш've hijacked route). put in controller:
private addcouponcodesviewmodel viewmodel; public addcouponcodescontroller(){ //get ipublished content ipublishedcontent content = umbraco.typedcontent(1225); //pass viewmodel viewmodel = new addcouponcodesviewmodel(content); routedata.datatokens["umbraco"] = content; } public actionresult index() { //return view etc }
but i'm getting
exception details: system.nullreferenceexception: object reference not set instance of object.
here:
source error(addcouponcodesviewmodel.cs): line 20: line 21: } line 22: public addcouponcodesviewmodel(ipublishedcontent content) line 23: : base(content) line 24: {
addcouponcoderendermodel.cs:
public class addcouponcodesviewmodel : rendermodel { public string test { get; set; } public list<string> tables { get; set; } public list<string> errors { get; set; } public addcouponcodesviewmodel(ipublishedcontent content, cultureinfo culture) : base(content, culture) { } public addcouponcodesviewmodel(ipublishedcontent content) : base(content) { }
and global.asax
public class global : umbracoapplication { protected override void onapplicationstarted(object sender, eventargs e) { base.onapplicationstarted(sender, e); bundleconfig.registerbundles(bundletable.bundles); //arearegistration.registerallareas(); //webapiconfig.register(globalconfiguration.configuration); //filterconfig.registerglobalfilters(globalfilters.filters); //routeconfig.registerroutes(routetable.routes); base.onapplicationstarting(sender, e); routetable.routes.maproute( "addcouponcodes", // route name "admin/{controller}/{action}/{id}", // url parameters new { controller = "addcouponcodes", action = "index", id = "" } // parameter defaults ); } }
the content published (i've checked , double checked), , node id correct.
what i'm trying here, route example.com/admin/{controller}/{action}/{parameter} routed, having problems connecting umbraconode (and class rendermodel requires ipublishcontent object parameter, i'm in no luck when trying pass anything)
could please me here, been stuck way many hours on :-(
to clarify, if hijacking route, means overriding way umbraco passes it's rendermodel
1 of it's published pages. can either globally overriding main rendermvccontroller
, or can override on documenttype
-specific basis. example, if have homepage doc type, create:
public homepagecontroller : rendermvccontroller { public override actionresult index(rendermodel model) { // create new rendermodel here, inheriting // rendermodel return currenttemplate(rendermodel); } }
this route calls homepage through 1 action. this, don't need define new routes in route table. , should override render model in action not in constructor.
your question confusing , it's not entirely clear trying achieve because:
- you have defined route, and
- in constructor calling
umbraco.typedcontent(1225)
retrieve specific published node
so ... if admin page trying route has been published umbraco (and doesn't sound has), create new controller name of page's document type , override render model in way described above.
however ... if admin page hasn't been published umbraco , want admin page access node data, have couple of options:
- create surface controller, inheriting surfacecontroller. give access umbraco context et al; or
- create standard controller (preferrably in area) , inject
contentcache
using autofac
e.g.:
builder.registercontrollers(typeof (admincontroller).assembly) .withparameter("contentcache", umbracocontext.current.contentcache);
- create standard controller (preferrably in area) , access node using umbraco's
contentservice
api, i.e.new umbraco.core.services.contentservice().getbyid(1225)
the difference between last 2 approaches that:
- injecting
contentcache
provides readonly quick access published content. - accessing
contentservice
provides read/write access nodes @ expense of speed querying database directly.
it depends on requirement is.
either way, worth taking time read through documentation hijacking umbraco routes, , @ least trying understand going on.
Comments
Post a Comment