Copied to clipboard

Flag this post as spam?

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


  • Travis Schoening 47 posts 173 karma points
    Feb 02, 2018 @ 15:45
    Travis Schoening
    0

    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

    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

    @hasQuerystring
    

    Thanks!

  • Kevin Jump 2342 posts 14889 karma points MVP 8x c-trib
    Feb 03, 2018 @ 11:02
    Kevin Jump
    0

    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

    public class MyDefaultController : RenderMvcController
    {
        public override ActionResult Index(RenderModel model)
        {
    
            ViewData.Add("HasQueryString", true);
            return base.Index(model);
        }
    }
    

    the viewdata object is persisted across the controller and the view so within your template you can get the values:

    bool query = false;
    if (ViewData.ContainsKey("HasQueryString"))
    {
        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...
            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.

  • Travis Schoening 47 posts 173 karma points
    Feb 03, 2018 @ 15:44
    Travis Schoening
    0

    Fantastic reply, thank you. Going to try those suggestions to see what works best. Appreciate it the in-depth reply!

  • Damien Holley 180 posts 541 karma points
    May 16, 2023 @ 23:49
    Damien Holley
    0

    You can as the models generated by modelsbuilder are partial classes, you can extend the model.

Please Sign in or register to post replies

Write your reply to:

Draft