I don't you can't add properties directly to the content Model (All the methods are internal), but what you can do is intercept the requests and change how the model is passed to your razor scripts.
You can replace the default render controller for Umbraco with another class that implements Umbraco.Web.Mvc.RenderController, and manipulate the models before they are sent to the views.
at this point you could do your checks for query strings, and then you can pass that information on via the viewbag or viewdata objects.
* you should check type and the like as well, example is for brevity
Other ways :
I would be inclined to add an Extension Method to the HtmlHelper class, that does the query string check .
public static bool HasSpecialQueryString(this HtmlHelper helper)
{
// do the checks...
return true;
}
then within a razor view you could do
var hasQuery = Html.HasSpecialQueryString();
then you've done no custom routing and the query string check only ever happens when you need it to.
or
You could in theory also implement your own RenderModel class, add the properties to that and add it on, but i think much of those classes are internal to Umbraco and you will end up implementing quite a lot of extra stuff.
You could also go compleatly off the render model and impliment your own MVC style controllers and pass whatever models you want to the views - again this will work, but you are stepping off the umbraco train a bit, and loosing the benefits.
Adding properties to content model
I'd like to add a simple property to every content item that looks to see if a specific querystring is being used in the URL such as this
While that is obviously easy enough to put in each view, it's hopefully unnecessary repetiveness...
Anyone know how to add this to content models so it's available on each content item simply as
Thanks!
Hi
I don't you can't add properties directly to the content Model (All the methods are internal), but what you can do is intercept the requests and change how the model is passed to your razor scripts.
You can replace the default render controller for Umbraco with another class that implements
Umbraco.Web.Mvc.RenderController
, and manipulate the models before they are sent to the views.at this point you could do your checks for query strings, and then you can pass that information on via the viewbag or viewdata objects.
Viewbag and ViewData are very similar, (see http://www.dotnettricks.com/learn/mvc/viewdata-vs-viewbag-vs-tempdata-vs-session)
Within a controller you could do your tests and add the result to the ViewDataObject
the viewdata object is persisted across the controller and the view so within your template you can get the values:
* you should check type and the like as well, example is for brevity
Other ways :
I would be inclined to add an Extension Method to the HtmlHelper class, that does the query string check .
then within a razor view you could do
then you've done no custom routing and the query string check only ever happens when you need it to.
or
You could in theory also implement your own
RenderModel
class, add the properties to that and add it on, but i think much of those classes are internal to Umbraco and you will end up implementing quite a lot of extra stuff.You could also go compleatly off the render model and impliment your own MVC style controllers and pass whatever models you want to the views - again this will work, but you are stepping off the umbraco train a bit, and loosing the benefits.
Fantastic reply, thank you. Going to try those suggestions to see what works best. Appreciate it the in-depth reply!
You can as the models generated by modelsbuilder are partial classes, you can extend the model.
is working on a reply...