Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
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
var hasQuerystring = Request.QueryString["querystringtolookfor"] != null;
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
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
public class MyDefaultController : RenderMvcController
public override ActionResult Index(RenderModel model)
the viewdata object is persisted across the controller and the view so within your template you can get the values:
bool query = false;
query = (bool)ViewData["HasQueryString"];
* 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...
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.
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!
is working on a reply...
Write your reply to:
Image will be uploaded when post is submitted